diff --git a/src/QmlControls/QmlObjectListModel.cc b/src/QmlControls/QmlObjectListModel.cc index 50c3c123aeec0a7bb56c35656da3f0a57b898cc2..a9222c686d83fb4dd6644b7109300184a01ede33 100644 --- a/src/QmlControls/QmlObjectListModel.cc +++ b/src/QmlControls/QmlObjectListModel.cc @@ -125,6 +125,22 @@ bool QmlObjectListModel::removeRows(int position, int rows, const QModelIndex& p return true; } +void QmlObjectListModel::move(int from, int to) +{ + if(0 <= from && from < count() && 0 <= to && to < count() && from != to) { + // Workaround to allow move item to the bottom. Done according to + // beginMoveRows() documentation and implementation specificity: + // https://doc.qt.io/qt-5/qabstractitemmodel.html#beginMoveRows + // (see 3rd picture explanation there) + if(from == to - 1) { + to = from++; + } + beginMoveRows(QModelIndex(), from, from, QModelIndex(), to); + _objectList.move(from, to); + endMoveRows(); + } +} + QObject* QmlObjectListModel::operator[](int index) { if (index < 0 || index >= _objectList.count()) { diff --git a/src/QmlControls/QmlObjectListModel.h b/src/QmlControls/QmlObjectListModel.h index e5b6b31c465a5b4ee21e4d1c63ec5949a48d576b..db7f7f1fa1aeab52e4117526db8d19ef9d76795b 100644 --- a/src/QmlControls/QmlObjectListModel.h +++ b/src/QmlControls/QmlObjectListModel.h @@ -46,6 +46,9 @@ public: bool contains (QObject* object) { return _objectList.indexOf(object) != -1; } int indexOf (QObject* object) { return _objectList.indexOf(object); } + /// Moves an item to a new position + void move(int from, int to); + QObject* operator[] (int i); const QObject* operator[] (int i) const; template T value (int index) { return qobject_cast(_objectList[index]); }