Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Unexpected behavior of QFileSystemModel
Forum Updated to NodeBB v4.3 + New Features

Unexpected behavior of QFileSystemModel

Scheduled Pinned Locked Moved Solved Qt for Python
13 Posts 2 Posters 1.8k Views
  • 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

    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?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #2

    @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
    0
    • JonBJ JonB

      @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 Offline
      M Offline
      midnightdim
      wrote on last edited by
      #3

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

      JonBJ 1 Reply Last reply
      0
      • M midnightdim

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

        @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
        0
        • JonBJ 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 Offline
          M Offline
          midnightdim
          wrote on last edited by
          #5

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

          JonBJ 1 Reply Last reply
          1
          • M midnightdim

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @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
            0
            • JonBJ JonB

              @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 Offline
              M Offline
              midnightdim
              wrote on last edited by
              #7

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

              JonBJ 1 Reply Last reply
              0
              • M midnightdim

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #8

                @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
                1
                • M Offline
                  M Offline
                  midnightdim
                  wrote on last edited by
                  #9

                  @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
                  1
                  • JonBJ JonB

                    @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 Offline
                    M Offline
                    midnightdim
                    wrote on last edited by
                    #10

                    @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
                    0
                    • M Offline
                      M Offline
                      midnightdim
                      wrote on last edited by
                      #11

                      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.

                      JonBJ 1 Reply Last reply
                      1
                      • M midnightdim

                        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.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #12

                        @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
                        1
                        • JonBJ 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 Offline
                          M Offline
                          midnightdim
                          wrote on last edited by
                          #13

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

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved