diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-01-13 10:39:15 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-02-02 09:40:09 +0100 |
commit | 1cb0e3dc5508359f5fc79eda79964db4ef8882c9 (patch) | |
tree | 8890a4093bb9a2fd38a2562c621cee6bce9aed84 /src | |
parent | c2f3f9def88c5d7b6a1f8aff49519cfbd39fe1d9 (diff) |
ListModel: Fix move handling in sync
When a ListModel is modified in a workerscript, we reconciliate the
state in ListModel::sync. However, the logic for moving elements was
wrong, causing crashes in endMoveRows due to invalid moves.
This patch ensures that elements are moved to the correct position.
Notably, whenever an element is moved, we must update the targetIndex of
all elements affected by that move.
Amends 3accc1dae76575120e71cadb547e961ecd50bcb0.
Task-number: QTBUG-85557
Change-Id: I1a1ffa43eab39ed2315f1916527d897b208c2c3b
Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io>
(cherry picked from commit 2996439993c587bc9c7169e4f152169f28247c21)
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/qmlmodels/qqmllistmodel.cpp | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/qmlmodels/qqmllistmodel.cpp b/src/qmlmodels/qqmllistmodel.cpp index 8830e08097..e392c9e323 100644 --- a/src/qmlmodels/qqmllistmodel.cpp +++ b/src/qmlmodels/qqmllistmodel.cpp @@ -446,7 +446,8 @@ bool ListModel::sync(ListModel *src, ListModel *target) // to ensure things are kept in the correct order, emit inserts and moves first. This shouls ensure all persistent // model indices are updated correctly int rowsInserted = 0; - for (int i = 0 ; i < target->elements.count() ; ++i) { + const int targetElementCount = target->elements.count(); + for (int i = 0 ; i < targetElementCount ; ++i) { ListElement *element = target->elements.at(i); ElementSync &s = elementHash.find(element->getUid()).value(); Q_ASSERT(s.srcIndex >= 0); @@ -456,13 +457,33 @@ bool ListModel::sync(ListModel *src, ListModel *target) if (s.targetIndex == -1) { targetModel->beginInsertRows(QModelIndex(), i, i); targetModel->endInsertRows(); + ++rowsInserted; } else { - targetModel->beginMoveRows(QModelIndex(), i, i, QModelIndex(), s.srcIndex); + bool validMove = targetModel->beginMoveRows(QModelIndex(), s.targetIndex, s.targetIndex, QModelIndex(), i); + Q_ASSERT(validMove); + Q_UNUSED(validMove); // fixes unused variable warning if asserts are disabled targetModel->endMoveRows(); + // fixup target indices of elements that still need to move + for (int j=i+1; j < targetElementCount; ++j) { + ListElement *eToFix = target->elements.at(j); + ElementSync &sToFix = elementHash.find(eToFix->getUid()).value(); + if (i < s.targetIndex) { + // element was moved down + if (sToFix.targetIndex > s.targetIndex || sToFix.targetIndex < i) + continue; // unaffected by reordering + else + sToFix.targetIndex += 1; + } else { + // element was moved up + if (sToFix.targetIndex < s.targetIndex || sToFix.targetIndex > i) + continue; // unaffected by reordering + else + sToFix.targetIndex -= 1; + } + } } } hasChanges = true; - ++rowsInserted; } if (s.targetIndex != -1 && !s.changedRoles.isEmpty()) { QModelIndex idx = targetModel->createIndex(i, 0); |