我希望为使用PyQt5创建的应用创建小部件。我希望用户能够在指定目录下的文件系统层次结构中选择文件的任何子集。我扩展了QFileSystemModel以允许大致遵循此
检查模型中的元素我希望用户能够在检查目录时(甚至在扩展子目录之前)修改目录内容的检查状态。
这样:
...在树状视图中的折叠节点的“内部”执行此操作:
我面临的问题是,QTreeView和QFileSystemModel似乎都是相互影响的,或者仅通过报告已查看的模型项来共同优化性能。在手动扩展视图中的子目录之前,无法遍历模型中的数据。
为了说明,我添加了一个打印回调,并将其传递给我的(递归)树遍历例程-打印任何索引具有多少个子代。 (请参阅附加的代码。)在双击树形视图中的子目录之前,该子目录没有任何子项:图像1 在我单击“一个”时报告以下内容:
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
...但是如果我展开视图-如图片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]-这涉及相同的类,但不是相同的问题
答案 0 :(得分:0)
根据设计,QFileSystemModel
不会加载所有项目,因为该任务非常繁重,另一方面,hasChildren()
指示它是否有子级作为子目录或文件,但是rowCount()
仅返回this report中讨论了由于设计问题而可见的子项。
因此,您不应使用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。