Treeview / QSortFilterProxyModel rendering problem..
-
Hi All
I'm just starting to toy with PyQt5, its just a simple program that recursively scans a path and then populates QStandardItemModel which is attached to a TreeView, its works OK, but when sorting the tree it hangs until complete (and given the size, it can be a few seconds).
So I read using QSortFilterProxyModel can help fix, which it does and UI no longer hangs.. But instead, I'm seeing some off behaviour, as you can see in the screenshot. I'm seeing gaps in the tree, there is no Row there (I've debugged the tree content and there is nothing), also the selection bar is not redrawing correctly (if I scroll the tree, this forces redraw correctly).
Has anybody else ran into this? I've upgrade both Qt and PyQT and the same problem is occuring. Has anybody got any thoughts on this? Is there anyway I can force a redraw on-selection? (it's a hack, but still)
Thanks
Kelvin -
Hi,
Out of curiosity, why not use QFileSystemMdel ?
-
Hi SGaist
Thanks for replying (to this and my last post).
The purpose of this program is to recurse the directory tree and show the size of all sub-content (a poor imitation of of TreeSize for Window), QFileSystemModel does not do this.
I'm trying to write code which leaves the UI responsive, both when scanning the directory tree and when sorting, the scanning works quite well, but the only way I've found not to freeze up the UI when sorting is via the QSortFilterProxyModel, but I immediately started having the problem and have spent 2 weeks bashing my head against a wall trying to find a solution.
-
Then what about moving the loading part in its own thread ?
-
I'm trying to write code which leaves the UI responsive, both when scanning the directory tree and when sorting, the scanning works quite well,
It's easy to leave the UI responsive during the scanning phase.
Am I right in guessing: during the scanning phase you read all file paths from the disk, but do not update the UI (though maybe it remains responsive)? Then you do the complete sort, without updating the UI? So that at the end you can show a view of all the files in the system, sorted by something arbitrary like size?
If you're trying to be like Win TreeSize, I'm thinking you'll want to read, sort & update the display all the time as you go along? I think as it reads directories it updates whatever it's sorted by as that is at that stage during the on-going reading. So "incomplete" or "incremental" sorting? If that's so I would be thinking of doing your own sorting as you go along, forcing the view to update sequentially?
P.S.
and given the size, it can be a few seconds
Not when I use TreeView on my
C:\
--- it can take minutes! -
@JonB @SGaist Thanks for taking the time to reply. Sorry I've done a bad job of explaining.. The issue is not with the scanning or sorting at scan time.
I'm using a QRunner thread which recursively scans the directory tree, then creates QStandardItem for each file/directory/symlink and puts them into a QStandardItemModel, which is associated with a QTreeView - The scanning and initial sorting (whilst scanning) work perfectly fine.
Cnce scanning is complete, a user can use the column headers to change the sorting (i.e. via Name or Type), if I rely upon the default sorting, it causes the UI to freeze whilst the sort is processed. If I use a QSortFilterProxyModel, this freezing does not happen (UI remains interactive and sort is much quicker), but, the problem is, I'm getting the on-selection glitch that I showed above (if I scroll the tree or resize the window, it forces a redraw and fixes it self).
I would provide the code.. but, it's a hell of a mess (this is not my day job, I'm just experimenting/playing in the evenings)
I've come to the conclusion its a bug / anomaly as I've got the same issue with both PyQT5 and PySide2.. So think I'm going to put this to bed, and move onto something else.