Using PyQt 5 I have been trying to find a way to get the old parent from a drag and drop operation on an item in a QTreeView situation using QStandardItemModel and QStandarItems.
I really want to avoid creating my own model if possible.
My current attempt is to store the current parent in the item when it is created as the "old parent" and it should not be updated in the move so I can reference it update the values in the old parent items and then update the "old parent" in the moved item to the new current parent.
I can not seem to get it to work though, here is the code I am trying to use to store the "old parent" when the item is created:
item.setData(parent.index(),(Qt.UserRole+3))
When I run this however I get the following error:
QVariant::save: unable to save type 'QModelIndex' (type id: 42).
and I can not reference the old parent at this point...
I found one reference using c++ and a lot of "pointer casting" but I could not figure out how to convert the code to Python and PyQt 5.
C++ Reference: https://forum.qt.io/topic/1690/qvariant-save-load-unable-to-save-type-x/19
Thanks for any help!
The model has some signals that fire whenever an item's children are inserted or removed, so these could be used to automatically update the item.
After some experimentation, I found that the signals need to be used with a queued connection, so that the model has a chance to fully update:
But other than that, the implementation is quite simple. There is no need to store any extra information in the items, as the updates can be performed dynamically.
Here is a basic demo script: