Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unexpected behavior of QFileSystemModel



  • Hello,

    I'm using QFileSystemModel to create a custom file browser with PySide6.
    I'm facing the following problem: for some folders (that I set via setRootPath) it works as I expect it - it shows subfolders first then files and columns sorting works (I enable it via setSortingEnabled(True)).
    However, for other folders it shows files and subfolders in one list sorted by name and columns sorting doesn't work.

    The interesting case is that I have a C:\Users\user\Downloads folder which is a symlink to a folder on another drive, let's call it D:\Downloads.
    When I set root path to "C:\Users\user\Downloads" it works fine, but when I set it to "D:\Downloads" it mixes subfolders and files and sorting doesn't work, although technically it's the the same folder with the same contents.

    How is this possible? How can it be fixed?



  • @midnightdim
    Windows doesn't really have "symlink"s. Let's start with: is the actual directory where you have the contents C:\Users\user\Downloads or D:\Downloads? Please be clear in your answer.

    Separately, are you able to test any of this from PySide2 instead of PySide6, in case it's a PySide6 issue, which is very new.



  • @JonB Thanks for checking. Can't post replies quickly because of my 0 reputation.

    The symlinks don't really matter. The problem is that the contents are shown differently for different folders, and that's weird.
    In case of the "symlinked" folder, the actual content is in D:\Downloads', andC:\Users\user\Downloads` just points to that location.

    I've just made an experiment. I have a folder on drive C:. When I specify it as the root, it shows fine and sorting works.
    I copied it to drive D:. When I specify it as the root, the problems occur.



  • @midnightdim
    This is not good! :( Assuming your filing system on D: is the same as on C:, plus it's a local drive, both of which I imagine are the case.

    Any chance of testing it from PySide2 or even PyQt5/6? If it's really an issue, I imagine it will lie in QFileSystemModel (perhaps under Qt6), rather than being anything Python/PySide/PyQt related....



  • @JonB I tried to quickly port to PySide2, but ran into some issues.
    I could port it to PyQt6, and I could reproduce this issue.
    So it looks like this problem lies deeper. I'll check if I can reports this bug in Qt bug tracker.



  • @midnightdim
    Shame you couldn't have tried PyQt5. Your usage looks OK, and QFileSystemModel must work right in versions of Qt prior to 6?



  • @JonB I found a bug that looks kind of similar: https://bugreports.qt.io/browse/QTBUG-12934
    But it's closed as invalid.



  • @midnightdim
    Yes, that is indeed your situation. But it's closed/invalid because you need to read what the comments say and make your QFileSystemModel::rootPath line up with your QTreeView::setRootIndex().



  • @JonB OK, this is exactly the same issue that was described in https://bugreports.qt.io/browse/QTBUG-12934

    When I set the root path to "" and have the full tree displayed, and then I expand the first drive C: all folders look as expected.
    However, all folders under other drives have files and folders mixed - exactly like on the screenshots attached to the issue.

    I don't fully understand the solution. The comment before the last one says:

    But it's not a bug imho.
    The user explicitly sets the QFileSystemModel::rootPath to be a sub-tree of the model (C:\ in this case).
    Now he is supposed to use QTreView::setRootIndex() to show only the sub-tree, D:'s sorting becomes a moot point because it's hidden.

    What exactly should be updated in the code to have this resolved? It's not clear from setRootIndex documentation for QTreeView.



  • @JonB Here's how I'm using it:

    path = r"D:\Downloads"
    self.model = QFileSystemModel()
    self.model.setRootPath((QDir.rootPath()))
    self.ui.treeView.setModel(self.model)
    self.ui.treeView.setRootIndex(self.model.index(path))
    

    It seems to correspond to what that ticket suggests.



  • OK, I found my mistake. It should be:

    path = r"D:\Downloads"
    self.model = QFileSystemModel()
    self.model.setRootPath(path)
    self.ui.treeView.setModel(self.model)
    self.ui.treeView.setRootIndex(self.model.index(path))
    

    I.e. root path should be set to the same folder. Thanks for your support.



  • @midnightdim
    Good stuff.

    As a small tip: Qt pathnames (for Qt functions expecting a pathname) are best always written using /s instead of \s, on every platform, even Windows. path = "D:/Downloads". The same for Python's pathlib. Tends to work with backslashes, but can be "gotchas", so use forward always.



  • @JonB Thanks. I use os.path.normpath everywhere to get rid of this kind of problems :)


Log in to reply