summaryrefslogtreecommitdiffstats
path: root/src/corelib/itemmodels/qstringlistmodel.cpp
diff options
context:
space:
mode:
authorLuca Beldi <v.ronin@yahoo.it>2018-08-22 16:42:45 +0100
committerLuca Beldi <v.ronin@yahoo.it>2018-08-23 05:51:55 +0000
commitda9aeb3e4ec63876e48d024772f0f0bd0b790157 (patch)
tree972fcab7e6f59e03c28fa2a90f4010678849b3a5 /src/corelib/itemmodels/qstringlistmodel.cpp
parentc1b15005c6594d0a0828190c858d9ab8ab3353b5 (diff)
Implement QStringListModel::moveRows
Implemented the virtual method moveRows to allow row movement [ChangeLog][QtCore][QStringListModel] Implemented moveRows Task-number: QTBUG-69807 Change-Id: I518f48a321bd755ab56f2fe84883d27324cc42ec Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src/corelib/itemmodels/qstringlistmodel.cpp')
-rw-r--r--src/corelib/itemmodels/qstringlistmodel.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/corelib/itemmodels/qstringlistmodel.cpp b/src/corelib/itemmodels/qstringlistmodel.cpp
index 567e6fa35e..ea77a42e34 100644
--- a/src/corelib/itemmodels/qstringlistmodel.cpp
+++ b/src/corelib/itemmodels/qstringlistmodel.cpp
@@ -249,6 +249,38 @@ bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent)
return true;
}
+/*!
+ \since 5.13
+ \reimp
+*/
+bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
+{
+ if (sourceRow < 0
+ || sourceRow + count - 1 >= rowCount(sourceParent)
+ || destinationChild <= 0
+ || destinationChild > rowCount(destinationParent)
+ || sourceRow == destinationChild - 1
+ || count <= 0) {
+ return false;
+ }
+ if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
+ return false;
+ /*
+ QList::move assumes that the second argument is the index where the item will end up to
+ i.e. the valid range for that argument is from 0 to QList::size()-1
+ QAbstractItemModel::moveRows when source and destinations have the same parent assumes that
+ the item will end up being in the row BEFORE the one indicated by destinationChild
+ i.e. the valid range for that argument is from 1 to QList::size()
+ For this reason we remove 1 from destinationChild when using it inside QList
+ */
+ destinationChild--;
+ const int fromRow = destinationChild < sourceRow ? (sourceRow + count - 1) : sourceRow;
+ while (count--)
+ lst.move(fromRow, destinationChild);
+ endMoveRows();
+ return true;
+}
+
static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2)
{
return s1.first < s2.first;