我希望为用PyQt5创建的应用程序创建一个小部件。我希望用户能够在指定目录下的文件系统层次结构中选择文件的任何子集。我对QFileSystemModel进行了扩展,允许检查模型中的元素,大致遵循以下步骤
示例。
我希望在选中目录时,用户能够修改目录内容的选中状态--甚至在子目录展开之前。
所以这个:

...does此树形视图中折叠节点的“遮罩”:

我面临的问题是,QTreeView --以及表面上的QFileSystemModel --都是各自的,或者仅仅通过报告已经查看过的模型项来优化性能。在手动展开视图中的子目录之前,我无法遍历模型中的数据。
为了举例说明,我添加了一个print回调,并将它传递给我的(递归)树--遍历例程--以打印任何索引有多少子元素。(见所附代码。)在双击树视图中的子one之前,它不报告子程序:图像1在单击“one”时报告如下:
tree clicked: /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one
|children|: 0如果我展开视图(如在...but图像2中),则会看到所有的子视图,如下所示:
tree clicked: /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one
|children|: 7
child[0]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_f
|children|: 0
child[1]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_e
|children|: 0
child[2]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_d
|children|: 0
child[3]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_c
|children|: 0
child[4]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_b
|children|: 0
child[5]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a
|children|: 6
child[0]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a/tfd
|children|: 0
child[1]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a/sgl
|children|: 0
child[2]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a/kjh
|children|: 0
child[3]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a/jyk
|children|: 0
child[4]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a/dgj
|children|: 0
child[5]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/one_a/..
|children|: 0
child[6]: recursing
traverseDirectory():
model printIndex(): /Users/caleb/dev/ML/cloudburst-ml/data/test_dir/one/..
|children|: 0如何引导子目录的加载,或者如何扩展模型对实际文件系统的反射?如何在用户单击视图小部件之前遍历QFileSystemModel的根路径?
这是我的代码:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
class FileTreeSelectorModel(QtWidgets.QFileSystemModel):
def __init__(self, parent=None, rootpath='/'):
QtWidgets.QFileSystemModel.__init__(self, None)
self.root_path = rootpath
self.checks = {}
self.nodestack = []
self.parent_index = self.setRootPath(self.root_path)
self.root_index = self.index(self.root_path)
self.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
self.directoryLoaded.connect(self._loaded)
def _loaded(self, path):
print('_loaded', self.root_path, self.rowCount(self.parent_index))
def data(self, index, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.CheckStateRole:
return QtWidgets.QFileSystemModel.data(self, index, role)
else:
if index.column() == 0:
return self.checkState(index)
def flags(self, index):
return QtWidgets.QFileSystemModel.flags(self, index) | QtCore.Qt.ItemIsUserCheckable
def checkState(self, index):
if index in self.checks:
return self.checks[index]
else:
return QtCore.Qt.Checked
def setData(self, index, value, role):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checks[index] = value
print('setData(): {}'.format(value))
return True
return QtWidgets.QFileSystemModel.setData(self, index, value, role)
def traverseDirectory(self, parentindex, callback=None):
print('traverseDirectory():')
callback(parentindex)
if self.hasChildren(parentindex):
print('|children|: {}'.format(self.rowCount(parentindex)))
for childRow in range(self.rowCount(parentindex)):
childIndex = parentindex.child(childRow, 0)
print('child[{}]: recursing'.format(childRow))
self.traverseDirectory(childIndex, callback=callback)
else:
print('no children')
def printIndex(self, index):
print('model printIndex(): {}'.format(self.filePath(index)))
class FileTreeSelectorDialog(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.root_path = '/Users/caleb/dev/ML/cloudburst-ml/data/test_dir/'
# Widget
self.title = "Application Window"
self.left = 10
self.top = 10
self.width = 1080
self.height = 640
self.setWindowTitle(self.title) #TODO: Whilch title?
self.setGeometry(self.left, self.top, self.width, self.height)
# Model
self.model = FileTreeSelectorModel(rootpath=self.root_path)
# self.model = QtWidgets.QFileSystemModel()
# View
self.view = QtWidgets.QTreeView()
self.view.setObjectName('treeView_fileTreeSelector')
self.view.setWindowTitle("Dir View") #TODO: Which title?
self.view.setAnimated(False)
self.view.setIndentation(20)
self.view.setSortingEnabled(True)
self.view.setColumnWidth(0,150)
self.view.resize(1080, 640)
# Attach Model to View
self.view.setModel(self.model)
self.view.setRootIndex(self.model.parent_index)
# Misc
self.node_stack = []
# GUI
windowlayout = QtWidgets.QVBoxLayout()
windowlayout.addWidget(self.view)
self.setLayout(windowlayout)
QtCore.QMetaObject.connectSlotsByName(self)
self.show()
@QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeView_fileTreeSelector_clicked(self, index):
print('tree clicked: {}'.format(self.model.filePath(index)))
self.model.traverseDirectory(index, callback=self.model.printIndex)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = FileTreeSelectorDialog()
sys.exit(app.exec_())我在这里看过几个链接
[1] -这并没有改变行为
[2] --这提出了一种不适合我的替代解决方案
[3] -这处理相同的类,但不是相同的问题。
发布于 2018-07-14 12:48:51
通过设计,QFileSystemModel并不会加载所有的项,因为该任务非常繁重,另一方面,hasChildren()指示它是否将子目录或文件作为子目录或文件,但是rowCount()只返回由于设计问题而可见的子项,这在本报告中讨论过。
因此,您不应该使用rowCount(),而是使用QDirIterator执行遍历目录的任务。
def traverseDirectory(self, parentindex, callback=None):
print('traverseDirectory():')
callback(parentindex)
if self.hasChildren(parentindex):
path = self.filePath(parentindex)
it = QtCore.QDirIterator(path, self.filter() | QtCore.QDir.NoDotAndDotDot)
while it.hasNext():
childIndex = self.index(it.next())
self.traverseDirectory(childIndex, callback=callback)
else:
print('no children')如果模型有很多级别,我建议您在另一个线程中实现该任务,因为它可以冻结GUI。
https://stackoverflow.com/questions/51338059
复制相似问题