1. <small id='KxeHf'></small><noframes id='KxeHf'>

      <legend id='KxeHf'><style id='KxeHf'><dir id='KxeHf'><q id='KxeHf'></q></dir></style></legend>

          <bdo id='KxeHf'></bdo><ul id='KxeHf'></ul>
        <i id='KxeHf'><tr id='KxeHf'><dt id='KxeHf'><q id='KxeHf'><span id='KxeHf'><b id='KxeHf'><form id='KxeHf'><ins id='KxeHf'></ins><ul id='KxeHf'></ul><sub id='KxeHf'></sub></form><legend id='KxeHf'></legend><bdo id='KxeHf'><pre id='KxeHf'><center id='KxeHf'></center></pre></bdo></b><th id='KxeHf'></th></span></q></dt></tr></i><div id='KxeHf'><tfoot id='KxeHf'></tfoot><dl id='KxeHf'><fieldset id='KxeHf'></fieldset></dl></div>
      1. <tfoot id='KxeHf'></tfoot>
      2. 为什么我的 QFileSystemModel QModelIndex 无法获取子节

        时间:2023-08-05
          <tfoot id='mctzB'></tfoot>

            • <bdo id='mctzB'></bdo><ul id='mctzB'></ul>

            • <legend id='mctzB'><style id='mctzB'><dir id='mctzB'><q id='mctzB'></q></dir></style></legend>
                <tbody id='mctzB'></tbody>

                <small id='mctzB'></small><noframes id='mctzB'>

                  <i id='mctzB'><tr id='mctzB'><dt id='mctzB'><q id='mctzB'><span id='mctzB'><b id='mctzB'><form id='mctzB'><ins id='mctzB'></ins><ul id='mctzB'></ul><sub id='mctzB'></sub></form><legend id='mctzB'></legend><bdo id='mctzB'><pre id='mctzB'><center id='mctzB'></center></pre></bdo></b><th id='mctzB'></th></span></q></dt></tr></i><div id='mctzB'><tfoot id='mctzB'></tfoot><dl id='mctzB'><fieldset id='mctzB'></fieldset></dl></div>

                • 本文介绍了为什么我的 QFileSystemModel QModelIndex 无法获取子节点信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在学习 pyqt 中的模型/视图架构,但是当我遵循 ,2) 指出:

                  <块引用>

                  缓存和性能

                  QFileSystemModel 不会获取任何文件或目录,直到调用 setRootPath().这将防止在此之前对文件系统进行的任何不必要的查询,例如列出 Windows 上的驱动器.

                  与 QDirModel 不同,QFileSystemModel 使用单独的线程来填充本身所以它不会导致主线程作为文件系统挂起正在被查询.调用 rowCount() 将返回 0,直到模型填充目录.

                  QFileSystemModel 保存一个包含文件信息的缓存.缓存是使用 QFileSystemWatcher 自动保持最新.

                  <小时><块引用>

                  QModelIndex QFileSystemModel::setRootPath(const QString &newPath)

                  集合模型正在监视的目录到 newPath 通过在其上安装文件系统观察程序.对文件的任何更改和此目录中的目录将反映在模型中.

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

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

                  强调我的

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

                  解决方案:

                  解决方案是在加载后请求信息,该信息将通过 的 noreferrer">directoryLoaded 信号QFileSystemModel:

                  from PyQt5.QtCore import pyqtSlot, QDir从 PyQt5.QtWidgets 导入 QApplication、QFileSystemModel、QPushButton类 DemoB(QPushButton):def __init__(self, parent=None):super().__init__(父)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(自我,目录):parentIndex = self.model.index(目录)对于范围内的行(self.model.rowCount(parentIndex)):index = self.model.index(row, 0, parentIndex)打印(索引,index.data())如果 __name__ == "__main__":导入系统应用程序 = QApplication(sys.argv)w = DemoB()w.show()sys.exit(app.exec_())

                  I am learn about Model/View architecture in pyqt, but when i follow the Using model indexes instruction and try to write a demo in pyqt5 style.The QModelIndex couldn't get child node information?

                  The code:

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

                  The result:

                  My folder:

                  解决方案

                  Explanation:

                  As the docs(1, 2) points out:

                  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.

                  emphasis mine

                  The loading process is executed in a different thread and the loading is done asynchronously, so at the time you make the request the model is not yet loaded.

                  Solution:

                  The solution is to request the information after it has been loaded that will be notified through the directoryLoaded signal of 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_())
                  

                  这篇关于为什么我的 QFileSystemModel QModelIndex 无法获取子节点信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 PyQt4 中相对于标签大小的变化保持按钮不 下一篇:Python PyQt5 将不再显示此消息复选框添加到 QMess

                  相关文章

                  最新文章

                  1. <i id='JqXgq'><tr id='JqXgq'><dt id='JqXgq'><q id='JqXgq'><span id='JqXgq'><b id='JqXgq'><form id='JqXgq'><ins id='JqXgq'></ins><ul id='JqXgq'></ul><sub id='JqXgq'></sub></form><legend id='JqXgq'></legend><bdo id='JqXgq'><pre id='JqXgq'><center id='JqXgq'></center></pre></bdo></b><th id='JqXgq'></th></span></q></dt></tr></i><div id='JqXgq'><tfoot id='JqXgq'></tfoot><dl id='JqXgq'><fieldset id='JqXgq'></fieldset></dl></div>
                    <tfoot id='JqXgq'></tfoot>

                  2. <small id='JqXgq'></small><noframes id='JqXgq'>

                    1. <legend id='JqXgq'><style id='JqXgq'><dir id='JqXgq'><q id='JqXgq'></q></dir></style></legend>

                        <bdo id='JqXgq'></bdo><ul id='JqXgq'></ul>