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

2024-04-23 18:11:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习pyqt中的模型/视图体系结构,但是当我遵循Using model indexes指令并尝试用pyqt5编写一个演示时风格。风格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())

结果是:

我的文件夹:


Tags: selfdataindexmodelinit风格ondef
1条回答
网友
1楼 · 发布于 2024-04-23 18:11:22

说明:

正如文档(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.

强调我的

加载过程在另一个线程中执行,加载是异步完成的,因此在发出请求时模型还没有加载。你知道吗

解决方案:

解决方案是在加载后请求信息,该信息将通过^{}^{}信号通知:

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

相关问题 更多 >