我的QFileSystemModel在PyQt中无法正常工作 [英] My QFileSystemModel doesn't work as expected in PyQt

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

问题描述

EDIT2 :model.hasChildren(parentIndex)返回True,但是model.rowCount(parentIndex)返回0. QFileSystemModel只是PyQt中的重要功能吗?

model.hasChildren(parentIndex) returns True, but model.rowCount(parentIndex) returns 0. Is QFileSystemModel just fubar in PyQt?

编辑:稍加修改,所有这些都与我使用QDirModel时完全一样.已弃用此方法,但是QFileSystemModel可能尚未在PyQt中完全实现吗?

With a bit of adaptation this all works exactly as it should if I use QDirModel. This is deprecated, but maybe QFileSystemModel hasn't been fully implemented in PyQt?

此刻,我正在学习Qt Model/View架构,并且发现了一些无法正常工作的东西.我有以下代码(改编自 Qt模型类):

I'm learning the Qt Model/View architecture at the moment, and I've found something that doesn't work as I'd expect it to. I've got the following code (adapted from Qt Model Classes):

from PyQt4 import QtCore, QtGui

model = QtGui.QFileSystemModel()

parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex) #prints True
print model.data(parentIndex).toString() #prints name of current directory

rows = model.rowCount(parentIndex)
print rows #prints 0 (even though the current directory has directory and file children)

问题:

这是PyQt的问题吗,我只是做错了什么,还是完全误解了QFileSystemModel?根据文档,model.rowCount(parentIndex)应该返回当前目录中的子代数. (我正在使用Python 2.6在Ubuntu上运行它)

The question:

Is this a problem with PyQt, have I just done something wrong, or am I completely misunderstanding QFileSystemModel? According to the documentation, model.rowCount(parentIndex) should return the number of children in the current directory. (I'm running this under Ubuntu with Python 2.6)

QFileSystemModel文档说它需要一个Gui应用程序的实例,因此我也将上述代码放在QWidget中,如下所示,但结果相同:

The QFileSystemModel docs say that it needs an instance of a Gui application, so I've also placed the above code in a QWidget as follows, but with the same result:

import sys
from PyQt4 import QtCore, QtGui

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        model = QtGui.QFileSystemModel()

        parentIndex = model.index(QtCore.QDir.currentPath())
        print model.isDir(parentIndex)
        print model.data(parentIndex).toString()

        rows = model.rowCount(parentIndex)
        print rows


def main():
    app = QtGui.QApplication(sys.argv)
    widget = Widget()
    widget.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

推荐答案

我已经解决了.

使用QFileSystemModel而不是QDirModel的原因是因为QFileSystemModel在单独的线程中从文件系统加载数据.这样做的问题是,如果您尝试在构造子代之后立即打印子代的数量,那就是它还不会加载子代.解决上述代码的方法是添加以下内容:

The reason to use QFileSystemModel as opposed to QDirModel is because QFileSystemModel loads the data from the filesystem in a separate thread. The problem with that is that if you try to print the number of children just after it's been constructed is that it won't have loaded the children yet. The way to fix the above code is to add the following:

self.timer = QtCore.QTimer(self)
self.timer.singleShot(1, self.printRowCount)

到构造函数的末尾,并添加一个printRowCount方法,该方法将打印正确数量的子代. ew.

to the end of the constructor, and add a printRowCount method which will print the correct number of children. Phew.

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

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