-1

I'm trying to do some basic Qt file manager app in Python 3.6 with PySide2. Code is like this:

class MainWidget(QWidget):
    startDir = "."

    def __init__(self):
        super().__init__()
        isDirselectDone = self.selectContentDir()
        if isDirselectDone:
            self.model = QFileSystemModel()
            self.model.setRootPath(self.startDir)
            self.tree = QTreeView()
            self.tree.setModel(self.model)
            self.tree.setSortingEnabled(True)
            self.tree.show()

    def selectContentDir(self):
        print("Selecing game content folder")

        self.startDir = QFileDialog.getExistingDirectory()

        if(len(self.startDir) == 0):
            print("Game content folder loading cancelled")
            return False

        print("Trying to load ", self.startDir)
        return True

My problem is that no matter what is the contents of the chosen directory, the view does not sort the files. I can click oh the headers, and the little arrows are changing, but everything remains in the order it was loaded.

I tried to look for solutions, but the answers either say that you just have to call the setSortingEnabled() and everything magically works (which is not the case), or they describe some voodoo magic rituals involving inheriting from QAbstractTreeSuperAncientGodItemViewDelegate, then re-implement half of the Qt library and finally creating a Tartarus-like maze by connecting slots and signals all over the place.

So what is the easiest / simplest way to make this sorting work according to latest Qt standards?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    How strange, I have modified your code a little and it works correctly for me: https://gist.github.com/eyllanesc/7f6419f94788ce95760847358e229a46 – eyllanesc Apr 23 '20 at 14:51
  • Just copy-pasting your version makes it work. However, the issue actually emerged in a much more complex code environment where I also have the QtApplication setup, and the proper set of the tree root index. What I posted is the most simple form of the problem where I could reproduce the issue without the full app complexity. Do you have any idea what could be the fundamental difference? Like the order of the setup calls maybe? – Péter Döbrei Apr 24 '20 at 08:21

1 Answers1

0

I found a solution for my case, based on the following assumptions: These three lines:

     model.setRootPath(directory)
     tree.setRootIndex(self.model.index(directory))
     tree.setSortingEnabled(True)

Seem they have to be in this specific order, and of course the model rootpath and the tree root index has to point to the same directory (during debugging I tried to set them to different ones, just to see what happens when the tree shows just a subset of the model data, but as expected it broke multiple things in the app).

Also, any custom lines (like handling your custom columns) has to happen between the root index setting, and the sorting enable. I don't know if this is really a general rule, it seems kind of arbitrary, but in my project it turned out to be crucial to call setSortingEnabled() as a last step, to make it work.

If your custom columns do not contain simple text (or anything not easily sortable, in my case a combobox) or they are hidden, you'd better exclude that column from the sorting. This is not mandatory, but for me it resulted in faster response time for the other columns.