python - 为什么我的 QFileSystemModel QModelIndex 无法获取子节点信息?

标签 python pyqt pyqt5 qfilesystemmodel

我正在学习 pyqt 中的模型/ View 架构,但是当我遵循 Using model indexes 时指令并尝试用pyqt5风格编写一个demo。QModelIndex无法获取子节点信息?

代码:

class DemoB(QPushButton):
    def __init__(self):
        super().__init__()

        self.clicked.connect(self.on_clicked)

    def on_clicked(self, checked):
        model = QFileSystemModel()
        model.setRootPath(QDir.homePath())
        parentIndex = model.index(QDir.homePath())
        print(parentIndex.data() )
        print(parentIndex, model.rowCount(parentIndex), QDir.homePath())
        for row in range(model.rowCount(parentIndex)):
            index = model.index(row, 0, parentIndex)
            print(index, index.data())

结果:

我的文件夹:

最佳答案

说明:

正如文档( 12 )指出的:

Caching and Performance

QFileSystemModel will not fetch any files or directories until setRootPath() is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows.

Unlike QDirModel, QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.

QFileSystemModel keeps a cache with file information. The cache is automatically kept up to date using the QFileSystemWatcher.


QModelIndex QFileSystemModel::setRootPath(const QString &newPath)

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.

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

Note: This function does not change the structure of the model or modify the data available to views. In other words, the "root" of the model is not changed to include only files and directories within the directory specified by newPath in the file system.

强调我的

加载过程在不同的线程中执行,并且加载是异步完成的,因此在您发出请求时,模型尚未加载。

解决方案:

解决方案是加载信息后请求信息,通过 directoryLoaded 通知。 QFileSystemModel的信号:

from PyQt5.QtCore import pyqtSlot, QDir
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QPushButton


class DemoB(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.clicked.connect(self.on_clicked)
        self.model = QFileSystemModel(self)
        self.model.directoryLoaded.connect(self.on_directoryLoaded)

    @pyqtSlot()
    def on_clicked(self):
        self.model.setRootPath(QDir.homePath())

    @pyqtSlot(str)
    def on_directoryLoaded(self, directory):
        parentIndex = self.model.index(directory)
        for row in range(self.model.rowCount(parentIndex)):
            index = self.model.index(row, 0, parentIndex)
            print(index, index.data())


if __name__ == "__main__":
    import sys

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

关于python - 为什么我的 QFileSystemModel QModelIndex 无法获取子节点信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58759075/

相关文章:

python - 如何停止函数

python - 如何过滤字符串?

python - 在Content disposition response file name-python/django中添加一个变量

python - PyQt QWizard 验证和按钮覆盖

python - 如何使用 tab 键从 QTableWidget 移动到下一个小部件

python-3.x - cx_Freeze : ImportError: No module named 'PyQt5.Qt'

python - 小部件从 QTreeview 中消失

qt - 在 QColumnView 中隐藏预览小部件?

python - PyQt5 QTextEdit自动补全

python - 如何依次播放视频的多个片段