From 88b9c6eb3f1dde9997d5e824450c9eaa2a36f8d5 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 26 Nov 2009 16:00:13 +0100 Subject: Don't crash via proxy model when moving QModelIndexes The layoutAboutToBeChanged signal gives proxies a chance to persist QModelIndexes. The d->itemsAboutToBeMoved method is part of the process of moving the persistent indexes. d->itemsAboutToBeMoved must be called after layoutAboutToBeChanged. Otherwise persistent indexes will not be updated. Merge-request: 2219 Reviewed-by: Olivier Goffart --- src/corelib/kernel/qabstractitemmodel.cpp | 2 +- .../qabstractitemmodel/tst_qabstractitemmodel.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qabstractitemmodel.cpp b/src/corelib/kernel/qabstractitemmodel.cpp index 10a61ca..9335085 100644 --- a/src/corelib/kernel/qabstractitemmodel.cpp +++ b/src/corelib/kernel/qabstractitemmodel.cpp @@ -2546,9 +2546,9 @@ bool QAbstractItemModel::beginMoveRows(const QModelIndex &sourceParent, int sour int destinationLast = destinationChild + (sourceLast - sourceFirst); d->changes.push(QAbstractItemModelPrivate::Change(destinationParent, destinationChild, destinationLast)); - d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical); emit rowsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild); emit layoutAboutToBeChanged(); + d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical); return true; } diff --git a/tests/auto/qabstractitemmodel/tst_qabstractitemmodel.cpp b/tests/auto/qabstractitemmodel/tst_qabstractitemmodel.cpp index 413419d..3736813 100644 --- a/tests/auto/qabstractitemmodel/tst_qabstractitemmodel.cpp +++ b/tests/auto/qabstractitemmodel/tst_qabstractitemmodel.cpp @@ -112,6 +112,8 @@ private slots: void testMoveWithinOwnRange_data(); void testMoveWithinOwnRange(); + void testMoveThroughProxy(); + void testReset(); @@ -1112,6 +1114,25 @@ void tst_QAbstractItemModel::testMoveSameParentUp() } } +void tst_QAbstractItemModel::testMoveThroughProxy() +{ + QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this); + proxy->setSourceModel(m_model); + + QList persistentList; + + persistentList.append(proxy->index(0, 0)); + persistentList.append(proxy->index(0, 0, proxy->mapFromSource(m_model->index(5, 0)))); + + ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this); + moveCommand->setNumCols(4); + moveCommand->setAncestorRowNumbers(QList() << 5); + moveCommand->setStartRow(0); + moveCommand->setEndRow(0); + moveCommand->setDestRow(0); + moveCommand->doCommand(); +} + void tst_QAbstractItemModel::testMoveToGrandParent_data() { QTest::addColumn("startRow"); -- cgit v0.12