python - 两个 QListView 框,一个显示文件夹中的文件,一个显示第一个 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_())
我想使用代码中描述的路径从我的文件夹中显示我的列表视图中的文件并能够选择它们,我选择的文件将显示在我的列表视图2中,但是,列表视图不显示此路径中的文件。有人可以帮我吗?
解决方案
文件没有显示,因为您没有rootPath
在QFileSystemModel
.
另一方面,第二个QListView
必须有一个模型,其中在选择或取消选择项目时添加或删除项目,为此您必须使用第一个的信号selectionChanged
,该信号传输所选和取消选择项目的信息。selectionModel()
QListView
要更改颜色,您必须获取QStandardItem
并使用角色的setData()
方法。Qt::BackgroundRole
在示例中,每秒颜色会随机更改
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_())
推荐阅读
- node.js - passeportjs 出错并在 express 应用上续集
- javascript - 如何在c#中动态地将多个纬度和经度传递给谷歌距离矩阵api?
- php - 想用react route功能,不能按参数分类吗?
- python - 在 Python 的主线程中使用线程锁时
- next.js - Shopify CLI 页面大小太大?
- android - 删除片段并重新添加后,PagingDataAdapter 停止加载
- node.js - 获取 Firestore 触发前后的文档数量 - 云函数
- python - Python selenium:编辑网页源代码
- discord - 如何防止机器人发送垃圾邮件?不和谐.py
- python - 火炬相当于 numpy.r_