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:\Users\Desktop\Project'
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?
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:\Users\Desktop\Project'
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_())