Important: Please read the Qt Code of Conduct -

QTreeView sorting questions

  • 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.setFilter(QDir.NoDot | QDir.AllEntries)
        self.ui.treeView.header().setSortIndicator(0, Qt.AscendingOrder)

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

    1. This line doesn't seem to do anything:
      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:
      folder A
      But it goes like this:
      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?


  • Lifetime Qt Champion


    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 ?

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

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

  • I submitted a more detailed explanation of my goals and problems here:

    Any advice is appreciated here or on Stack Overflow.

  • 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.setFilter(QDir.NoDot | QDir.AllEntries)
        model.sort(0, Qt.SortOrder.AscendingOrder)
        sorting_model = SortingModel()
        view.tree_view.header().setSortIndicator(0, Qt.AscendingOrder)

Log in to reply