pyqt QFileSystemModel rowCount

2019-08-06 02:39发布

问题:

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

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

回答1:

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