带有 QFileSystemModel 的 QTreeView 无法正常工作 [英] QTreeView with QFileSystemModel is not working properly

查看:70
本文介绍了带有 QFileSystemModel 的 QTreeView 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 QFileSystemModel 根路径,然后将其设置为 QTreeView 模型,但是如果我尝试查找特定文件的索引,它会给我 D:我确定文件在那里!

I set QFileSystemModel root path and then set it as QTreeView model, but if I try to find index of a speciffic file it is giving me D: I am sure the file is there !

self.model = QtWidgets.QFileSystemModel()
self.model.setNameFilters(['*.ma'])
self.model.setFilter(QtCore.QDir.Files)#QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
self.model.setNameFilterDisables(False)
self.model.setRootPath(path)
self.tree_local_file.setModel(self.model)
self.tree_local_file.setRootIndex(self.model.index(path))

# ...
# then
# ...

for i in range(self.model.rowCount()):
    index = self.model.index(i, 0)
    file_name = str(self.model.fileName(index))
    file_path = str(self.model.filePath(index))
    print(file_path) # this gave me -> D:/
    if file_name == master_file_name:
        self.tree_local_file.setCurrentIndex(index)
        self.open_file()
        break
# or

index = (self.model.index(master_file_name[1]))
print(self.model.filePath(index)) # this is giving me nothing

推荐答案

如果 docs 已审核:

QModelIndex QFileSystemModel::setRootPath(const QString &newPath)

将模型正在监视的目录设置为newPath by在其上安装文件系统观察程序.对文件的任何更改和此目录中的目录将反映在模型中.

Sets the directory that is being watched by the model to newPath by installing a file system watcher on it. Any changes to files and directories within this directory will be reflected in the model.

如果路径改变,rootPathChanged() 信号将被发射.

If the path is changed, the rootPathChanged() signal will be emitted.

注意:此功能不会改变模型的结构或修改可用于视图的数据.换句话说,根"模型未更改为仅包含文件和目录文件系统中由 newPath 指定的目录.

(强调我的)

据了解,模型的根从未改变,因此如果您想访问根路径下的项目,您必须获取与该路径关联的 QModelIndex,然后获取您的孩子.

From what is understood that the root of the model has never changed, so if you want to access the items below the rootPath you must obtain the QModelIndex associated with that path and then get your children.

另一方面,QFileSystemModel 在另一个线程中执行其任务以避免 GUI 的某些阻塞,因此在更改 rootPath 时您将无法获得足够的路由,但至少您必须等待发出 directoryLoaded 信号以指示线程上的工作结束了.

On the other hand, QFileSystemModel performs its tasks in another thread to avoid some blocking of the GUI so you will not get an adequate route as you change the rootPath but at least you have to wait for the directoryLoaded signal to be issued indicating that the work done on the thread is over.

考虑到上述情况,一个可能的解决方案是:

Considering the above a possible solution is:

from PyQt5 import QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.tree_local_file = QtWidgets.QTreeView()
        self.setCentralWidget(self.tree_local_file)

        path = "/foo/path/"

        self.model = QtWidgets.QFileSystemModel()
        self.model.setNameFilters(["*.ma"])
        self.model.setFilter(
            QtCore.QDir.Files
        )  # QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
        self.model.setNameFilterDisables(False)
        self.model.setRootPath(path)
        self.tree_local_file.setModel(self.model)
        self.tree_local_file.setRootIndex(self.model.index(path))

        self.model.directoryLoaded.connect(self.onDirectoryLoaded)

    @QtCore.pyqtSlot()
    def onDirectoryLoaded(self):
        root = self.model.index(self.model.rootPath())
        for i in range(self.model.rowCount(root)):
            index = self.model.index(i, 0, root)
            file_name = self.model.fileName(index)
            file_path = self.model.filePath(index)
            print(file_path)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

这篇关于带有 QFileSystemModel 的 QTreeView 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆