I’ve got a custom model inherited from QTreeView. I’ve enabled drag and drop and can currently drop an item onto the tree. However, you can currently drop onto either an existing item or between items. I would like to restrict this so that you can only drop onto existing items.
I’ve set DragDropOverwriteMode to true (actually this is the default for QTreeView). However, this doesn’t stop you from dropping between items – it just means you can also drop onto existing items.
I know that I can ignore the “insert” drops in dropMimeData (which I am reimplementing), by checking whether row and column are valid (drops onto existing items have row and column set to -1 and parent set to the current item) and I am doing this. However, I would prefer not to get these drops. Ie. I would like it so that you are always dropping over either the item above or the item below, never between items.
Any ideas?
Thanks for any advice,
Giles
You” need to catch the drag enter events by reimplementing the
dragEnterEventmethod in your custom view. The example from the Qt docs is:In your case, you would probably need to compare the x and y position in the event with the x and y position of the closest item or something similar and reject or accept the proposed action based on that information.
From the
QAbstractItemModel::dropMimeDatadocumentation:Which I have interpreted to mean that the view should reject the drop if it’s not something that’s supported by an underlying model, such as yours.