[PyQt] QTreeView + sort + delete = crash

John F Sturtz john at sturtz.org
Tue May 7 21:12:23 BST 2019


Well, that isn't quite all.  Before and after the sorting, I emit the layoutAboutToBeChanged and layoutChanged signals.  In what will probably prove to be a state of unwarranted naiveté, I thought that was enough to tell Qt things had changed.  (I guess as I think about it, that only tells Qt that things have changed, not how ...)

I did spend some time trying to set up a QSortFilterProxyModel.  In the time I spent, I did not succeed in wrapping my mind around it, but I could try harder.  It's been a couple weeks since I looked at it, but as I recall, I think my hangup seemed to be -- if you're calling a method to modify the model contents (e.g., begin/endRemovRows() or begin/endInsertRows()), do you call the method on the proxy, or the original model?

But I can look at it again, if a direct sort can't (easily) be made to work.  If you happen to know of any particularly good tutorials or examples on setting up a proxy, I'd be happy to hear about them.

Thanks again!

/John

On 5/7/2019 12:21:24 PM, Kyle Altendorf <sda at fstab.net> wrote:
Is this really all you do when you sort?

# Sort children
def sort_children(self):
self.children.sort(key=lambda x: x.desc())
return

Yes, that would upset Qt very much. You would need to do a series of
moves or a restructure or... I'm forgetting the terms offhand. But
yes, you have to tell Qt when stuff moves around. This would be part of
why it's useful to let an entirely stock Qt object like a proxy take
care of it for you.

Cheers,
-kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190507/9e4fa4a0/attachment.html>


More information about the PyQt mailing list