Two QListView box one showing files in a folder and one shows selected files from the first QListview












1














import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
hlay = QHBoxLayout(self)

self.listview = QListView()
self.listview2 = QListView()

hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

path = r'C:UsersDesktopProject'

self.fileModel = QFileSystemModel()
self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)

self.listview.setRootIndex(self.fileModel.index(path))


if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())


I want to display the files in my listview from my folder with path described in the code and able to
select them, the files I selected will be displayed in my listview2, However, the listview doesn't show
the files in this path. Can anyone help me with it?










share|improve this question
























  • Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown?
    – eyllanesc
    Nov 12 '18 at 21:51










  • Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted?
    – eyllanesc
    Nov 13 '18 at 20:07










  • @eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView
    – Cindy
    Nov 13 '18 at 20:10










  • Now it's clearer, what information other than the name of the file do you want the second QListView to have?
    – eyllanesc
    Nov 13 '18 at 20:12










  • You say: By now I just want the files, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView?
    – eyllanesc
    Nov 13 '18 at 20:20
















1














import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
hlay = QHBoxLayout(self)

self.listview = QListView()
self.listview2 = QListView()

hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

path = r'C:UsersDesktopProject'

self.fileModel = QFileSystemModel()
self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)

self.listview.setRootIndex(self.fileModel.index(path))


if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())


I want to display the files in my listview from my folder with path described in the code and able to
select them, the files I selected will be displayed in my listview2, However, the listview doesn't show
the files in this path. Can anyone help me with it?










share|improve this question
























  • Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown?
    – eyllanesc
    Nov 12 '18 at 21:51










  • Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted?
    – eyllanesc
    Nov 13 '18 at 20:07










  • @eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView
    – Cindy
    Nov 13 '18 at 20:10










  • Now it's clearer, what information other than the name of the file do you want the second QListView to have?
    – eyllanesc
    Nov 13 '18 at 20:12










  • You say: By now I just want the files, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView?
    – eyllanesc
    Nov 13 '18 at 20:20














1












1








1







import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
hlay = QHBoxLayout(self)

self.listview = QListView()
self.listview2 = QListView()

hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

path = r'C:UsersDesktopProject'

self.fileModel = QFileSystemModel()
self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)

self.listview.setRootIndex(self.fileModel.index(path))


if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())


I want to display the files in my listview from my folder with path described in the code and able to
select them, the files I selected will be displayed in my listview2, However, the listview doesn't show
the files in this path. Can anyone help me with it?










share|improve this question















import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
hlay = QHBoxLayout(self)

self.listview = QListView()
self.listview2 = QListView()

hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

path = r'C:UsersDesktopProject'

self.fileModel = QFileSystemModel()
self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files)

self.listview.setRootIndex(self.fileModel.index(path))


if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())


I want to display the files in my listview from my folder with path described in the code and able to
select them, the files I selected will be displayed in my listview2, However, the listview doesn't show
the files in this path. Can anyone help me with it?







python pyqt pyqt5 qlistview qfilesystemmodel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 21:37









eyllanesc

73.6k103056




73.6k103056










asked Nov 12 '18 at 21:31









Cindy

7016




7016












  • Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown?
    – eyllanesc
    Nov 12 '18 at 21:51










  • Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted?
    – eyllanesc
    Nov 13 '18 at 20:07










  • @eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView
    – Cindy
    Nov 13 '18 at 20:10










  • Now it's clearer, what information other than the name of the file do you want the second QListView to have?
    – eyllanesc
    Nov 13 '18 at 20:12










  • You say: By now I just want the files, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView?
    – eyllanesc
    Nov 13 '18 at 20:20


















  • Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown?
    – eyllanesc
    Nov 12 '18 at 21:51










  • Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted?
    – eyllanesc
    Nov 13 '18 at 20:07










  • @eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView
    – Cindy
    Nov 13 '18 at 20:10










  • Now it's clearer, what information other than the name of the file do you want the second QListView to have?
    – eyllanesc
    Nov 13 '18 at 20:12










  • You say: By now I just want the files, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView?
    – eyllanesc
    Nov 13 '18 at 20:20
















Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown?
– eyllanesc
Nov 12 '18 at 21:51




Do you want that when a file is selected then it is added to the other QListView or only what is selected is shown?
– eyllanesc
Nov 12 '18 at 21:51












Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted?
– eyllanesc
Nov 13 '18 at 20:07




Are you asking me ?, I have not yet pointed out a solution so I do not understand your question. I still have doubts about what you want, for example let's say that 5 files of 100 have been selected and they are in the second QListView, and let's say that one of those 5 files is deselected. Should that same item be eliminated in the second QListView? if not, how would an item in the second QListView be deleted?
– eyllanesc
Nov 13 '18 at 20:07












@eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView
– Cindy
Nov 13 '18 at 20:10




@eyllanesc yes exactly what you said, when you select files, selected files appear in the second QListView and when you deselect, deselected ones will disappear from the second QListView
– Cindy
Nov 13 '18 at 20:10












Now it's clearer, what information other than the name of the file do you want the second QListView to have?
– eyllanesc
Nov 13 '18 at 20:12




Now it's clearer, what information other than the name of the file do you want the second QListView to have?
– eyllanesc
Nov 13 '18 at 20:12












You say: By now I just want the files, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView?
– eyllanesc
Nov 13 '18 at 20:20




You say: By now I just want the files, a file are many things: the name of the file, the path of the file, the weight of the file, the date of creation, date of modification, etc., what do you want it to be? show in the second QListView? What do you think about using the information contained in the second QListView?
– eyllanesc
Nov 13 '18 at 20:20












1 Answer
1






active

oldest

votes


















1














The files are not displayed because you have not set a rootPath in the QFileSystemModel.



On the other hand the second QListView must have a model where items are added or removed as they are selected or deselected, for this you must use the selectionChanged signal of selectionModel() of the first QListView, that signal transports the information of the selected and deselected items.



To change the color you must obtain the QStandardItem and use the setData() method with the Qt::BackgroundRole role. In the example in each second the color is changed randomly



import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
self.listview = QtWidgets.QListView()
self.listview2 = QtWidgets.QListView()

path = r'C:UsersDesktopProject'

self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.selectionModel().selectionChanged.connect(self.on_selectionChanged)
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

self.model = QtGui.QStandardItemModel(self)
self.listview2.setModel(self.model)

hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

timer = QtCore.QTimer(self, interval=1000, timeout=self.test_color)
timer.start()

def on_selectionChanged(self, selected, deselected):
roles = (QtCore.Qt.DisplayRole,
QtWidgets.QFileSystemModel.FilePathRole,
QtWidgets.QFileSystemModel.FileNameRole,
QtCore.Qt.DecorationRole)

for ix in selected.indexes():
it = QtGui.QStandardItem(ix.data())
for role in roles:
it.setData(ix.data(role), role)
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)
self.model.appendRow(it)

filter_role = QtWidgets.QFileSystemModel.FilePathRole
for ix in deselected.indexes():
for index in self.model.match(ix.parent(), filter_role, ix.data(filter_role), -1, QtCore.Qt.MatchExactly):
self.model.removeRow(index.row())

def test_color(self):
if self.model.rowCount() > 0:
n_e = random.randint(0, self.model.rowCount())
rows_red = random.sample(range(self.model.rowCount()), n_e)
for row in range(self.model.rowCount()):
it = self.model.item(row)
if row in rows_red:
it.setData(QtGui.QColor("red"), QtCore.Qt.BackgroundRole)
else:
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())





share|improve this answer





















  • Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
    – Cindy
    Nov 13 '18 at 21:32










  • @Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
    – eyllanesc
    Nov 13 '18 at 21:35













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270404%2ftwo-qlistview-box-one-showing-files-in-a-folder-and-one-shows-selected-files-fro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The files are not displayed because you have not set a rootPath in the QFileSystemModel.



On the other hand the second QListView must have a model where items are added or removed as they are selected or deselected, for this you must use the selectionChanged signal of selectionModel() of the first QListView, that signal transports the information of the selected and deselected items.



To change the color you must obtain the QStandardItem and use the setData() method with the Qt::BackgroundRole role. In the example in each second the color is changed randomly



import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
self.listview = QtWidgets.QListView()
self.listview2 = QtWidgets.QListView()

path = r'C:UsersDesktopProject'

self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.selectionModel().selectionChanged.connect(self.on_selectionChanged)
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

self.model = QtGui.QStandardItemModel(self)
self.listview2.setModel(self.model)

hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

timer = QtCore.QTimer(self, interval=1000, timeout=self.test_color)
timer.start()

def on_selectionChanged(self, selected, deselected):
roles = (QtCore.Qt.DisplayRole,
QtWidgets.QFileSystemModel.FilePathRole,
QtWidgets.QFileSystemModel.FileNameRole,
QtCore.Qt.DecorationRole)

for ix in selected.indexes():
it = QtGui.QStandardItem(ix.data())
for role in roles:
it.setData(ix.data(role), role)
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)
self.model.appendRow(it)

filter_role = QtWidgets.QFileSystemModel.FilePathRole
for ix in deselected.indexes():
for index in self.model.match(ix.parent(), filter_role, ix.data(filter_role), -1, QtCore.Qt.MatchExactly):
self.model.removeRow(index.row())

def test_color(self):
if self.model.rowCount() > 0:
n_e = random.randint(0, self.model.rowCount())
rows_red = random.sample(range(self.model.rowCount()), n_e)
for row in range(self.model.rowCount()):
it = self.model.item(row)
if row in rows_red:
it.setData(QtGui.QColor("red"), QtCore.Qt.BackgroundRole)
else:
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())





share|improve this answer





















  • Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
    – Cindy
    Nov 13 '18 at 21:32










  • @Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
    – eyllanesc
    Nov 13 '18 at 21:35


















1














The files are not displayed because you have not set a rootPath in the QFileSystemModel.



On the other hand the second QListView must have a model where items are added or removed as they are selected or deselected, for this you must use the selectionChanged signal of selectionModel() of the first QListView, that signal transports the information of the selected and deselected items.



To change the color you must obtain the QStandardItem and use the setData() method with the Qt::BackgroundRole role. In the example in each second the color is changed randomly



import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
self.listview = QtWidgets.QListView()
self.listview2 = QtWidgets.QListView()

path = r'C:UsersDesktopProject'

self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.selectionModel().selectionChanged.connect(self.on_selectionChanged)
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

self.model = QtGui.QStandardItemModel(self)
self.listview2.setModel(self.model)

hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

timer = QtCore.QTimer(self, interval=1000, timeout=self.test_color)
timer.start()

def on_selectionChanged(self, selected, deselected):
roles = (QtCore.Qt.DisplayRole,
QtWidgets.QFileSystemModel.FilePathRole,
QtWidgets.QFileSystemModel.FileNameRole,
QtCore.Qt.DecorationRole)

for ix in selected.indexes():
it = QtGui.QStandardItem(ix.data())
for role in roles:
it.setData(ix.data(role), role)
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)
self.model.appendRow(it)

filter_role = QtWidgets.QFileSystemModel.FilePathRole
for ix in deselected.indexes():
for index in self.model.match(ix.parent(), filter_role, ix.data(filter_role), -1, QtCore.Qt.MatchExactly):
self.model.removeRow(index.row())

def test_color(self):
if self.model.rowCount() > 0:
n_e = random.randint(0, self.model.rowCount())
rows_red = random.sample(range(self.model.rowCount()), n_e)
for row in range(self.model.rowCount()):
it = self.model.item(row)
if row in rows_red:
it.setData(QtGui.QColor("red"), QtCore.Qt.BackgroundRole)
else:
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())





share|improve this answer





















  • Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
    – Cindy
    Nov 13 '18 at 21:32










  • @Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
    – eyllanesc
    Nov 13 '18 at 21:35
















1












1








1






The files are not displayed because you have not set a rootPath in the QFileSystemModel.



On the other hand the second QListView must have a model where items are added or removed as they are selected or deselected, for this you must use the selectionChanged signal of selectionModel() of the first QListView, that signal transports the information of the selected and deselected items.



To change the color you must obtain the QStandardItem and use the setData() method with the Qt::BackgroundRole role. In the example in each second the color is changed randomly



import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
self.listview = QtWidgets.QListView()
self.listview2 = QtWidgets.QListView()

path = r'C:UsersDesktopProject'

self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.selectionModel().selectionChanged.connect(self.on_selectionChanged)
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

self.model = QtGui.QStandardItemModel(self)
self.listview2.setModel(self.model)

hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

timer = QtCore.QTimer(self, interval=1000, timeout=self.test_color)
timer.start()

def on_selectionChanged(self, selected, deselected):
roles = (QtCore.Qt.DisplayRole,
QtWidgets.QFileSystemModel.FilePathRole,
QtWidgets.QFileSystemModel.FileNameRole,
QtCore.Qt.DecorationRole)

for ix in selected.indexes():
it = QtGui.QStandardItem(ix.data())
for role in roles:
it.setData(ix.data(role), role)
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)
self.model.appendRow(it)

filter_role = QtWidgets.QFileSystemModel.FilePathRole
for ix in deselected.indexes():
for index in self.model.match(ix.parent(), filter_role, ix.data(filter_role), -1, QtCore.Qt.MatchExactly):
self.model.removeRow(index.row())

def test_color(self):
if self.model.rowCount() > 0:
n_e = random.randint(0, self.model.rowCount())
rows_red = random.sample(range(self.model.rowCount()), n_e)
for row in range(self.model.rowCount()):
it = self.model.item(row)
if row in rows_red:
it.setData(QtGui.QColor("red"), QtCore.Qt.BackgroundRole)
else:
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())





share|improve this answer












The files are not displayed because you have not set a rootPath in the QFileSystemModel.



On the other hand the second QListView must have a model where items are added or removed as they are selected or deselected, for this you must use the selectionChanged signal of selectionModel() of the first QListView, that signal transports the information of the selected and deselected items.



To change the color you must obtain the QStandardItem and use the setData() method with the Qt::BackgroundRole role. In the example in each second the color is changed randomly



import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs)
self.listview = QtWidgets.QListView()
self.listview2 = QtWidgets.QListView()

path = r'C:UsersDesktopProject'

self.fileModel = QtWidgets.QFileSystemModel(self)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.selectionModel().selectionChanged.connect(self.on_selectionChanged)
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

self.model = QtGui.QStandardItemModel(self)
self.listview2.setModel(self.model)

hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.listview)
hlay.addWidget(self.listview2)

timer = QtCore.QTimer(self, interval=1000, timeout=self.test_color)
timer.start()

def on_selectionChanged(self, selected, deselected):
roles = (QtCore.Qt.DisplayRole,
QtWidgets.QFileSystemModel.FilePathRole,
QtWidgets.QFileSystemModel.FileNameRole,
QtCore.Qt.DecorationRole)

for ix in selected.indexes():
it = QtGui.QStandardItem(ix.data())
for role in roles:
it.setData(ix.data(role), role)
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)
self.model.appendRow(it)

filter_role = QtWidgets.QFileSystemModel.FilePathRole
for ix in deselected.indexes():
for index in self.model.match(ix.parent(), filter_role, ix.data(filter_role), -1, QtCore.Qt.MatchExactly):
self.model.removeRow(index.row())

def test_color(self):
if self.model.rowCount() > 0:
n_e = random.randint(0, self.model.rowCount())
rows_red = random.sample(range(self.model.rowCount()), n_e)
for row in range(self.model.rowCount()):
it = self.model.item(row)
if row in rows_red:
it.setData(QtGui.QColor("red"), QtCore.Qt.BackgroundRole)
else:
it.setData(QtGui.QColor("green"), QtCore.Qt.BackgroundRole)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 21:14









eyllanesc

73.6k103056




73.6k103056












  • Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
    – Cindy
    Nov 13 '18 at 21:32










  • @Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
    – eyllanesc
    Nov 13 '18 at 21:35




















  • Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
    – Cindy
    Nov 13 '18 at 21:32










  • @Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
    – eyllanesc
    Nov 13 '18 at 21:35


















Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
– Cindy
Nov 13 '18 at 21:32




Thank you so much! just one quick question, the ix in selected.indexes() are PyQt5.QtCore.QModelIndex object, how do I retreive the file name of the ix
– Cindy
Nov 13 '18 at 21:32












@Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
– eyllanesc
Nov 13 '18 at 21:35






@Cindy use ix.data() or ix.data(QtWidgets.QFileSystemModel.FilePathRole) or ix.data(QtWidgets.QFileSystemModel.FileNameRole)
– eyllanesc
Nov 13 '18 at 21:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270404%2ftwo-qlistview-box-one-showing-files-in-a-folder-and-one-shows-selected-files-fro%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine