How to Accept/Reject an InternalMove in a QTreeView
-
I have a QTreeView that contains N parent items and each parent has one or more children. I want to support internal moves (reordering) of children within each parent but not between parents. I could add a unique mime-type to each class of children and do a full drag/drop setup that would allow me to accept/reject a drop based on that mime-type. If I have to do a full drag/drop setup, what exactly does a "move" look like in terms of events and can I stop that chain of events once it has begun with something like an event filter, or would I have a lot of state to restore?
I feel like there should be an easier way that includes using QAbstractItemView::InternalMove, but I just can't seem to see it. All I need to check is if the moving child's parent model index agrees with the accepting parent model index.
Many thanks for any suggestions.
-
I should clarify that I'm backending with just QStandardItemModel at this point.
One of my ideas is to derive a model from QStandardItemModel and then reimplement moveRows(), which I assume is called on an "internal move" type operation. In there I can evaluate the source and destination of the move and if it is acceptable, call the parent class implementation. If not, return false.
-
It doesn't look like moveRows() is ever called. Upon some further reading and then some code experimentation it appears an "internal move" is implemented as an insertRows() and removeRows() combination, which are both virtual. So I can grab the item being moved in an override insertRows(), evaluate the validity during removeRows() and then decide if the remove and insert should happen. If so, I will just call the QStandardItemModel functions to perform the operation.
-
Hi and welcome to devnet,
Since it's GUI related, you should implement a custom tree view. There you have the logic that will check whether the index under the mouse is a valid target.