pyqt QFileSystemModel rowCount [英] pyqt QFileSystemModel rowCount

查看:41
本文介绍了pyqt QFileSystemModel rowCount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过有关 QFileSystemModel rowCount 未按预期工作的帖子 (ex1, ex2),但是我似乎错过了什么.即使列表显示更多……即使等待 10 秒后,以下代码也始终报告 rowCount 为 1.我在这里错过了什么?

I've seen posts about QFileSystemModel rowCount not working as expected (ex1, ex2), but I seem to be missing something. The following code always reports a rowCount of 1 even though the list shows more..even after waiting 10 seconds. What am I missing here?

import os, sys
from PyQt5 import QtWidgets, QtCore


class TestWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.model = QtWidgets.QFileSystemModel()
        self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
        self.path = os.path.expanduser('~')
        self.model.setRootPath(self.path)
        view = QtWidgets.QListView()
        view.setModel(self.model)
        view.setRootIndex(self.model.index(self.path))
        self.setCentralWidget(view)
        self.model.directoryLoaded.connect(self._loaded)
        QtCore.QTimer.singleShot(10000, self._really_loaded)

    def _loaded(self):
        print('_loaded', self.path, self.model.rowCount())  # Always returns 1 here? even though there are more rows displayed

    def _really_loaded(self):
        print('_really_loaded', self.path, self.model.rowCount())  # 10 seconds later...Always returns 1 here? even tho there are more rows displayed


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    test = TestWindow()
    test.show()
    sys.exit(app.exec_())

...为了理智...这里的代码与 pyqt4 相同,结果相同

...for sanity..here's the same code as with pyqt4 with same result

import os, sys
from PyQt4 import QtGui, QtCore


class TestWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.model = QtGui.QFileSystemModel()
        self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
        self.path = os.path.expanduser('~')
        self.model.setRootPath(self.path)
        view = QtGui.QListView()
        view.setModel(self.model)
        view.setRootIndex(self.model.index(self.path))
        self.setCentralWidget(view)
        self.model.directoryLoaded.connect(self._loaded)
        QtCore.QTimer.singleShot(10000, self._really_loaded)

    def _loaded(self):
        print('_loaded', self.path, self.model.rowCount())  # Always returns 1 here? even though there are more rows displayed

    def _really_loaded(self):
        print('_really_loaded', self.path, self.model.rowCount())  # 10 seconds later...Always returns 1 here? even tho there are more rows displayed


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    test = TestWindow()
    test.show()
    sys.exit(app.exec_())

推荐答案

你必须传递你要分析的item的索引,如果你想知道你有多少item,使用返回setRootPath的索引().

You must pass the index of the item you want to analyze, if you want to know how many items you have, use the index that returns setRootPath().

import os, sys
from PyQt5 import QtWidgets, QtCore


class TestWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.model = QtWidgets.QFileSystemModel()
        self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
        self.path = os.path.expanduser('~')
        self.parentIndex  = self.model.setRootPath(self.path)
        view = QtWidgets.QListView()
        view.setModel(self.model)
        view.setRootIndex(self.model.index(self.path))
        self.setCentralWidget(view)
        self.model.directoryLoaded.connect(self._loaded)
        QtCore.QTimer.singleShot(10000, self._really_loaded)

    def _loaded(self, path):
        print('_loaded', self.path, self.model.rowCount(self.parentIndex))  # Always returns 1 here? even though there are more rows displayed

    def _really_loaded(self):
        print('_really_loaded', self.path, self.model.rowCount(self.parentIndex))  # 10 seconds later...Always returns 1 here? even tho there are more rows displayed


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    test = TestWindow()
    test.show()
    sys.exit(app.exec_())

这篇关于pyqt QFileSystemModel rowCount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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