<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QTreeView sorting questions]]></title><description><![CDATA[<p dir="auto">I'm building a customized file browser with PySide6, and I ran into a number of issues with sorting.</p>
<p dir="auto">Here's the part that initializes the model and the tree view:</p>
<pre><code>    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)
</code></pre>
<p dir="auto">Now here are the problems that I'd like to solve.</p>
<ol>
<li>
<p dir="auto">This line doesn't seem to do anything:<br />
<code>self.model.sort(0,Qt.SortOrder.AscendingOrder)</code><br />
But this one does the actual sorting:<br />
<code>self.ui.treeView.header().setSortIndicator(0, Qt.AscendingOrder)</code><br />
I would like to have the data be sorted by the model and appear sorted in the tree view.<br />
How do I do that?<br />
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.</p>
</li>
<li>
<p dir="auto">I keep two dots <code>..</code> 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 <code>!something</code> subfolder in it, two dots get under it.<br />
I.e. I would like to have it this way:<br />
<code>..</code><br />
<code>!something</code><br />
<code>folder A</code><br />
But it goes like this:<br />
<code>!something</code><br />
<code>..</code><br />
<code>folder A</code><br />
I understand that it's because <code>!something</code> is less than <code>..</code> 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?</p>
</li>
</ol>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.qt.io/topic/123836/qtreeview-sorting-questions</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 17:39:27 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/123836.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Feb 2021 17:04:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTreeView sorting questions on Tue, 16 Feb 2021 12:44:18 GMT]]></title><description><![CDATA[<p dir="auto">Found the solution and posted it to StackOverflow. Pasting it here as well.</p>
<pre><code>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
</code></pre>
<p dir="auto">The code for initializing view and model:</p>
<pre><code>    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)
</code></pre>
]]></description><link>https://forum.qt.io/post/644777</link><guid isPermaLink="true">https://forum.qt.io/post/644777</guid><dc:creator><![CDATA[midnightdim]]></dc:creator><pubDate>Tue, 16 Feb 2021 12:44:18 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView sorting questions on Tue, 16 Feb 2021 07:14:26 GMT]]></title><description><![CDATA[<p dir="auto">I submitted a more detailed explanation of my goals and problems here: <a href="https://stackoverflow.com/questions/66220009/custom-file-browser-implementation-on-pyside6" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/66220009/custom-file-browser-implementation-on-pyside6</a></p>
<p dir="auto">Any advice is appreciated here or on Stack Overflow.</p>
]]></description><link>https://forum.qt.io/post/644690</link><guid isPermaLink="true">https://forum.qt.io/post/644690</guid><dc:creator><![CDATA[midnightdim]]></dc:creator><pubDate>Tue, 16 Feb 2021 07:14:26 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView sorting questions on Tue, 16 Feb 2021 04:36:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Thanks.</p>
<ol>
<li>So does it mean that it doesn't make sense to set sorting on the model level at all?<br />
I wrote half a second, not several seconds, but it's still noticeable.<br />
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.</li>
<li>When I comment out all sorting code the folder still appears sorted, but the other way, like so:<br />
<code>folder A</code><br />
<code>..</code><br />
<code>!something</code></li>
</ol>
<p dir="auto">Basically I would like to keep <code>..</code> "always on top", is there a way to do this with QFileSystemModel / QTreeView?</p>
]]></description><link>https://forum.qt.io/post/644681</link><guid isPermaLink="true">https://forum.qt.io/post/644681</guid><dc:creator><![CDATA[midnightdim]]></dc:creator><pubDate>Tue, 16 Feb 2021 04:36:04 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView sorting questions on Mon, 15 Feb 2021 19:19:15 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<ol>
<li>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.<br />
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 ?</li>
<li>Do you have the same issue when not sorting at all ?</li>
</ol>
]]></description><link>https://forum.qt.io/post/644644</link><guid isPermaLink="true">https://forum.qt.io/post/644644</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 15 Feb 2021 19:19:15 GMT</pubDate></item></channel></rss>