代码之家  ›  专栏  ›  技术社区  ›  Skilldrick

我的qFileSystemModel在Pyqt中不能按预期工作

  •  1
  • Skilldrick  · 技术社区  · 15 年前

    编辑2: model.hasChildren(parentIndex) 收益率 True 但是 model.rowCount(parentIndex) 收益率 0 . qfilesystemmodel是否只是pyqt中的fubar?

    编辑: 如果我使用qdirmodel,只要稍加调整,这一切就会完全正常工作。这是不赞成的,但也许QfileSystemModel没有在Pyqt中完全实现?


    我现在正在学习qt模型/视图体系结构,我发现有些东西并不像我期望的那样工作。我有以下代码(改编自 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(父索引) 应返回当前目录中的子目录数。(我用python 2.6在Ubuntu下运行这个程序)

    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()
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   Skilldrick    15 年前

    我已经解决了。

    与qdirmodel不同,使用qfilesystemmodel的原因是qfilesystemmodel在单独的线程中从文件系统加载数据。问题是,如果您在构建之后尝试打印子级的数目,那么它还没有加载子级。修复上述代码的方法是添加以下内容:

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

    在构造函数的末尾,添加一个printrowcount方法,该方法将打印正确数量的子级。呸。

        2
  •  1
  •   serge_gubenko    15 年前

    由于您已经了解了这个问题,所以您只需对您的模型进行一些额外的思考: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事件上正常工作。

    当做

    推荐文章