两个 QListView 框,一个显示文件夹中的文件,一个显示从第一个 QListview 中选择的文件

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

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_())

我想使用代码中描述的路径在我的文件夹中显示我的列表视图中的文件,并且能够 select 他们,我 selected 的文件将显示在我的 listview2 中,但是,listview 不显示 此路径中的文件。有人可以帮我吗?

文件没有显示,因为你没有在QFileSystemModel中设置rootPath

另一方面,第二个 QListView 必须有一个模型,其中项目在选择或取消选择时添加或删除,为此您必须使用 [=15= 的 selectionChanged 信号] 的第一个 QListView,该信号传输选定和取消选定项目的信息。

要更改颜色,您必须获得 QStandardItem 并使用具有 Qt::BackgroundRole 角色的 setData() 方法。在每秒的示例中,颜色随机更改

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_())