gpt4 book ai didi

python - 两个 QListView 框,一个显示文件夹中的文件,一个显示第一个 QListview 中选定的文件

转载 作者:行者123 更新时间:2023-12-01 08:46:48 34 4
gpt4 key购买 nike

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

我想用代码中描述的路径显示文件夹中 ListView 中的文件,并且能够选择它们,我选择的文件将显示在我的listview2中,但是,listview不显示该路径中的文件。谁能帮我解决一下吗?

最佳答案

由于您尚未在 QFileSystemModel 中设置 rootPath,因此未显示文件。

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

要更改颜色,您必须获取 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_())

关于python - 两个 QListView 框,一个显示文件夹中的文件,一个显示第一个 QListview 中选定的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53270404/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com