manatttta manatttta - 1 month ago 16
C++ Question

qt QAbstractItemModel drag&drop for moving items performs remove/insert

I created a class derived from

QAbrstractItemModel
to be displayed under a
QTreeView
. I need drag&drop operations to reorder items with the mouse.

However, I noticed that when I do this, the items are moved by removing the indices and then creating new ones under the new position. But I reimplemented the
moveRows()
method.

What can be wrong? Or is this the expected behaviour?

Answer

The problem is that moveRows (and all its companion functions) has been introduced in Qt 5. Drag and drop support for QAbstractItemModel subclasses comes from Qt 4, and even in Qt 5 it unfortunately still consists of a insert + a remove, because we don't want to break Qt 4 projects when they get ported to Qt 5.

Hence, Qt 5 doesn't make any use of moveRows (e.g. see here).

Long term, Qt will need to find a way to make use of the move operations on models, necessarily as an opt-in feature (we can't break code!). The question is really "who will do this huge job"...