Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Unexpected behavior of QFileSystemModel

    Qt for Python
    2
    13
    632
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      midnightdim last edited by

      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?

      JonB 1 Reply Last reply Reply Quote 1
      • JonB
        JonB @midnightdim last edited by

        @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.

        M 1 Reply Last reply Reply Quote 0
        • M
          midnightdim @JonB last edited by

          @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', and C:\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.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @midnightdim last edited by JonB

            @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....

            M 1 Reply Last reply Reply Quote 0
            • M
              midnightdim @JonB last edited by

              @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.

              JonB 1 Reply Last reply Reply Quote 1
              • JonB
                JonB @midnightdim last edited by

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

                M 1 Reply Last reply Reply Quote 0
                • M
                  midnightdim @JonB last edited by

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

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @midnightdim last edited by

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

                    M 1 Reply Last reply Reply Quote 1
                    • M
                      midnightdim last edited by

                      @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.

                      1 Reply Last reply Reply Quote 1
                      • M
                        midnightdim @JonB last edited by

                        @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.

                        1 Reply Last reply Reply Quote 0
                        • M
                          midnightdim last edited by

                          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.

                          JonB 1 Reply Last reply Reply Quote 1
                          • JonB
                            JonB @midnightdim last edited by JonB

                            @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.

                            M 1 Reply Last reply Reply Quote 1
                            • M
                              midnightdim @JonB last edited by

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

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post