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. QTreeView sorting questions
Forum Updated to NodeBB v4.3 + New Features

QTreeView sorting questions

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.9k Views 1 Watching
  • 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 Offline
    M Offline
    midnightdim
    wrote on last edited by
    #1

    I'm building a customized file browser with PySide6, and I ran into a number of issues with sorting.

    Here's the part that initializes the model and the tree view:

        self.model = QFileSystemModel()
        self.model.setRootPath(path)
        self.model.setFilter(QDir.NoDot | QDir.AllEntries)
        self.model.sort(0,Qt.SortOrder.AscendingOrder)
        self.ui.treeView.setModel(self.model)
        self.ui.treeView.setRootIndex(self.model.index(path))
        self.ui.treeView.header().setSortIndicator(0, Qt.AscendingOrder)
        self.ui.treeView.setSortingEnabled(True)
    

    Now here are the problems that I'd like to solve.

    1. This line doesn't seem to do anything:
      self.model.sort(0,Qt.SortOrder.AscendingOrder)
      But this one does the actual sorting:
      self.ui.treeView.header().setSortIndicator(0, Qt.AscendingOrder)
      I would like to have the data be sorted by the model and appear sorted in the tree view.
      How do I do that?
      Another issue which seems to be related to this is that when switching between folders the files/folders don't appear on the right places, it takes about half a second for them to be sorted.

    2. I keep two dots .. entry for browsing up the parent folder. I would like to keep two dots always on top, as all the file browsers do. However, if I have a folder with !something subfolder in it, two dots get under it.
      I.e. I would like to have it this way:
      ..
      !something
      folder A
      But it goes like this:
      !something
      ..
      folder A
      I understand that it's because !something is less than .. in terms of standard sorting and I probably have to override it with QSortFilterProxyModel. If I do, what exactly should be done? Or maybe there's a simpler workaround?

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      1. The model controls it however the view's initialisation resets it to a known state by the view to ensure that for example you do not have a sorting indicator that says ascending while the content shows descending.
        As for the folder content, QFileSystemModel is asynchronous and does not load the whole content of your filesystem for memory and performance reasons. However several seconds sounds big. Are you folders heavy ?
      2. Do you have the same issue when not sorting at all ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        1. The model controls it however the view's initialisation resets it to a known state by the view to ensure that for example you do not have a sorting indicator that says ascending while the content shows descending.
          As for the folder content, QFileSystemModel is asynchronous and does not load the whole content of your filesystem for memory and performance reasons. However several seconds sounds big. Are you folders heavy ?
        2. Do you have the same issue when not sorting at all ?
        M Offline
        M Offline
        midnightdim
        wrote on last edited by
        #3

        @SGaist Thanks.

        1. So does it mean that it doesn't make sense to set sorting on the model level at all?
          I wrote half a second, not several seconds, but it's still noticeable.
          The interesting fact here is that if I use QFileSystemModel directly it doesn't seem to happen - the folders appear sorted. But when I implement my customization (I'm adding another column with some extra data) there's this slight delay with sorting.
        2. When I comment out all sorting code the folder still appears sorted, but the other way, like so:
          folder A
          ..
          !something

        Basically I would like to keep .. "always on top", is there a way to do this with QFileSystemModel / QTreeView?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          midnightdim
          wrote on last edited by
          #4

          I submitted a more detailed explanation of my goals and problems here: https://stackoverflow.com/questions/66220009/custom-file-browser-implementation-on-pyside6

          Any advice is appreciated here or on Stack Overflow.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            midnightdim
            wrote on last edited by
            #5

            Found the solution and posted it to StackOverflow. Pasting it here as well.

            class SortingModel(QSortFilterProxyModel):
                def lessThan(self, source_left: QModelIndex, source_right: QModelIndex):
                    file_info1 = self.sourceModel().fileInfo(source_left)
                    file_info2 = self.sourceModel().fileInfo(source_right)       
                    
                    if file_info1.fileName() == "..":
                        return self.sortOrder() == Qt.SortOrder.AscendingOrder
            
                    if file_info2.fileName() == "..":
                        return self.sortOrder() == Qt.SortOrder.DescendingOrder
                            
                    if (file_info1.isDir() and file_info2.isDir()) or (file_info1.isFile() and file_info2.isFile()):
                        return super().lessThan(source_left, source_right)
            
                    return file_info1.isDir() and self.sortOrder() == Qt.SortOrder.AscendingOrder
            

            The code for initializing view and model:

                model = QFileSystemModel()
                model.setRootPath('.')
                model.setFilter(QDir.NoDot | QDir.AllEntries)
                model.sort(0, Qt.SortOrder.AscendingOrder)
                sorting_model = SortingModel()
                sorting_model.setSourceModel(model)
                view.tree_view.setModel(sorting_model)
                view.tree_view.setRootIndex(sorting_model.mapFromSource(model.index('.')))
                view.tree_view.header().setSortIndicator(0, Qt.AscendingOrder)
                view.tree_view.setSortingEnabled(True)
            
            1 Reply Last reply
            1

            • Login

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