我的 QFileSystemModel 在 PyQt 中无法按预期工作

发布于 2024-08-29 13:48:31 字数 1687 浏览 3 评论 0原文

编辑2: model.hasChildren(parentIndex) 返回True,但model.rowCount(parentIndex) 返回0 。 QFileSystemModel 只是 PyQt 中的 fubar 吗?

编辑:经过一些调整,如果我使用 QDirModel,这一切都可以完全正常工作。这已被弃用,但也许 QFileSystemModel 尚未在 PyQt 中完全实现?


我目前正在学习 Qt 模型/视图架构,但我发现有些东西不能按我的预期工作。我有以下代码(改编自 Qt 模型类):

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) 应该返回当前目录中的子级数。 (我在 Ubuntu 下使用 Python 2.6 运行这个)

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

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

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

EDIT: 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?


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)

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2024-09-05 13:48:31

我已经解决了。

使用 QFileSystemModel 而不是 QDirModel 的原因是因为 QFileSystemModel 在单独的线程中从文件系统加载数据。问题是,如果您在构建后尝试打印子级的数量,那么它还没有加载子级。修复上述代码的方法是将以下代码添加

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

到构造函数的末尾,并添加 printRowCount 方法,该方法将打印正确的子级数。唷。

I've solved it.

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)

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

朕就是辣么酷 2024-09-05 13:48:31

既然您已经弄清楚了,那么只需对您的模型发生的情况进行一些额外的思考即可: QFileSystemModel::rowCount 从visibleChildren 集合中返回行;我想您已经正确地识别了问题:在检查行数时,它尚未填充。我已经改变了你的例子,没有使用计时器;请检查它是否适合您:

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

        self.model = QtGui.QFileSystemModel()
        self.model.setRootPath(QtCore.QDir.currentPath())

    def checkParent(self):
        parentIndex = self.model.index(QtCore.QDir.currentPath())      

        print self.model.isDir(parentIndex)
        print self.model.data(parentIndex).toString()

        rows = self.model.rowCount(parentIndex)
        print "row count:", rows

def main():
    app = QtGui.QApplication(sys.argv)
    widget = Widget()
    widget.show()
    app.processEvents(QtCore.QEventLoop.AllEvents)  
    widget.checkParent()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我相信在屏幕上显示构建的小部件之后,您的代码应该在任何 UI 事件上都能正常

工作

Since you've already figured it out, just a couple of extra thoughts on what was going on with your model: QFileSystemModel::rowCount returns rows from the visibleChildren collection; I guess you're correctly identified the problem: at the time when you're checking row count it was not yet populated. I've changed your example without using timers; pls, check if it works for you:

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

        self.model = QtGui.QFileSystemModel()
        self.model.setRootPath(QtCore.QDir.currentPath())

    def checkParent(self):
        parentIndex = self.model.index(QtCore.QDir.currentPath())      

        print self.model.isDir(parentIndex)
        print self.model.data(parentIndex).toString()

        rows = self.model.rowCount(parentIndex)
        print "row count:", rows

def main():
    app = QtGui.QApplication(sys.argv)
    widget = Widget()
    widget.show()
    app.processEvents(QtCore.QEventLoop.AllEvents)  
    widget.checkParent()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

I believe your code should work correctly on any UI event after widget constructed is shown on the screen

regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文