4

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)?

Lithy
  • 817
  • 12
  • 23
  • I'm thinking that the problem is that `moveRows` is a Qt 5 API, and DND was implemented in Qt 4 times via insert+remove... – peppe Nov 03 '17 at 00:06
  • You mean it'd be a bug then? DIdn't they adapt the DnD API? How was the MoveAction implemented with Qt 4? – Lithy Nov 03 '17 at 08:56
  • As I said, via insert/remove. I'm not sure if it would qualify as a bug, in the sense that Qt 4 software would still work when ported to Qt 5 (such old software wouldn't have `moveRows` overridden, because it didn't exist back then...). But by all means please file a bug report so it can be discussed there. – peppe Nov 04 '17 at 13:35

1 Answers1

0

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.

pasbi
  • 2,037
  • 1
  • 20
  • 32
  • 1
    This leads to problems on systems where animations are displays. On Mac returning false will result in a reject animation being triggered (the dragged item will return to it's previous place visually). – Herr von Wurst Apr 08 '21 at 11:15