Skip to content Skip to sidebar Skip to footer

Pyqt Qfilesystemmodel Rowcount

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 thou

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

Post a Comment for "Pyqt Qfilesystemmodel Rowcount"