0

I am a little bit blown away with some result I obtained. I dont know what I am missing regarding the Qt library or python itself. I want to count the lines of my model, I have this code snippet which works, but sometimes the rows are properly retrieved and other times (in the init method and the program itself) 0 is obtained. What is wrong with this?

Find my working code snippet (based on: pyqt QFileSystemModel rowCount):

from PyQt5.QtWidgets import QApplication, QFileSystemModel, QSplitter, QTreeView, QListView, QMainWindow, QFrame
from PyQt5.QtCore import QTimer
import sys


class TestWindow(QSplitter):
    def __init__(self):
        QFrame.__init__(self)
        # self.splitter = QSplitter()
        self.model = QFileSystemModel()
        # model.setRootPath(QDir.currentPath())
        self.parentIndex = self.model.setRootPath("C:/Users/Luis/Documents/python/pyqt5")
        self.treeView = QTreeView(self)
        self.treeView.setModel(self.model)
        # tree.setRootIndex(model.index(QDir.currentPath()))
        self.treeView.setRootIndex(self.model.index("C:/Users/Luis/Documents/python/pyqt5"))
        self.listView = QListView(self)
        self.listView.setModel(self.model)
        # list.setRootIndex(model.index(QDir.currentPath()))
        self.listView.setRootIndex(self.model.index("C:/Users/Luis/Documents/python/pyqt5"))
        print('in the init method: ', self.model.rowCount(self.parentIndex))
        self.setWindowTitle("Two views onto the same directory model")
        # self.splitter.show()
        self.model.directoryLoaded.connect(self._loaded)
        QTimer.singleShot(10000, self._really_loaded)

    def _loaded(self):
        print('_loaded', self.model.rowCount(self.parentIndex))

    def _really_loaded(self):
        print('_really_loaded', self.model.rowCount(self.parentIndex))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    test = TestWindow()
    print('before showing the app', test.model.rowCount(test.parentIndex))
    test.show()
    print('after showing app', test.model.rowCount(test.parentIndex))
    sys.exit(app.exec_())

And my output:

enter image description here

You can put a working path of yours instead of mine to try out the code. I expected to obtain the number of rows of the model wherever it was asked in the code. Why sometimes its 0? Thanks for your comments.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • 1
    QFileSystemModel is a special model since it handles many items so that for efficiency reasons it will initially be empty and that it will be loaded when you establish a rootpath, so the number of rows does not match the number of folders / files until the directoryLoaded signal be emited – eyllanesc Dec 20 '19 at 02:47
  • OK, That would explain that in the __init__(self) equals 0. But in the program, handling the window instance itself (test.model.rowCount(test.parentIndex) the signal was already emitted, moreover I can see the files in the UI myself. Why? If I want to make a change in the view of my data, how would I access the rowcount info of the model then? – rustyBucketBay Dec 20 '19 at 09:50
  • 1
    `print('after showing app', test.model.rowCount(test.parentIndex))` does not run when the GUI is displayed since show() does not cause the GUI to be displayed but notifies Qt that it is desired to display the GUI, and the events are executed in the asynchronous part so it is only a moment after executing exec_() would result in the number of rows, so in conclusion: before directoryLoaded is emitted the value will not be correct but after directoryLoaded is correct. – eyllanesc Dec 20 '19 at 22:58

0 Answers0