Why does QAbstractItemView removes elements after a MoveAction drag and drop?

394 Views Asked by At

I noticed in the source code of QAbstractItemView that the method void startDrag(Qt::DropActions supportedActions) calls some clearOrRemove() private after a drag whose type is MoveAction(), which removes selected items.

Point is, when the drop action occured in the same view, my models implements the action using moveRows(), so the rows are moved and then clearOrRemove() removes them as if they were the original rows.

How can I prevent this last removal? Did I miss the idiomatic way of implementing a move-only model (meaning that items can be moved but not added/removed)?

1

There are 1 best solutions below

1
pasbi On

I ran into the very same problem and found a workaround:

It's very likely that you've overridden the QAbstractItemModel::dropMimeData-method of your model. That method is supposed to return true if the dopped mime data was handled by that method and false otherwise. The trick is to return false if action was a MoveAction, even though the data was handled correctly (implement the handling inside that method). That the view thinks the drop was not successful and hence doesn't remove the dragged items.

Maybe, hacking the return value yields some issues in more complex setups, but it works my simple case.