diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2011-11-10 20:34:46 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-11 12:24:48 +0100 |
commit | 999196e3369417969d4a561710d68c8bb1786c47 (patch) | |
tree | ac06b705e439c3ae2b327389054c6e4b0f6e8ab8 | |
parent | 0b293e4afc3a9d25336e862509a46ad024dccf04 (diff) |
Add API to clear the current index. Symmetric with clearing selection.
Change-Id: I08070f4fdf26898d5b6edd5259f011f9b3c75512
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
3 files changed, 42 insertions, 1 deletions
diff --git a/src/widgets/itemviews/qitemselectionmodel.cpp b/src/widgets/itemviews/qitemselectionmodel.cpp index 423a5802c2..996b12f805 100644 --- a/src/widgets/itemviews/qitemselectionmodel.cpp +++ b/src/widgets/itemviews/qitemselectionmodel.cpp @@ -1118,8 +1118,16 @@ void QItemSelectionModel::select(const QItemSelection &selection, QItemSelection */ void QItemSelectionModel::clear() { - Q_D(QItemSelectionModel); clearSelection(); + clearCurrentIndex(); +} + +/*! + Clears the current index. Emits currentChanged(). + */ +void QItemSelectionModel::clearCurrentIndex() +{ + Q_D(QItemSelectionModel); QModelIndex previous = d->currentIndex; d->currentIndex = QModelIndex(); if (previous.isValid()) { diff --git a/src/widgets/itemviews/qitemselectionmodel.h b/src/widgets/itemviews/qitemselectionmodel.h index cfa71d0ef2..ea0528a590 100644 --- a/src/widgets/itemviews/qitemselectionmodel.h +++ b/src/widgets/itemviews/qitemselectionmodel.h @@ -202,6 +202,7 @@ public Q_SLOTS: virtual void reset(); void clearSelection(); + virtual void clearCurrentIndex(); Q_SIGNALS: void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); diff --git a/tests/auto/widgets/itemviews/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/widgets/itemviews/qitemselectionmodel/tst_qitemselectionmodel.cpp index 19386d342c..6f805a04d2 100644 --- a/tests/auto/widgets/itemviews/qitemselectionmodel/tst_qitemselectionmodel.cpp +++ b/tests/auto/widgets/itemviews/qitemselectionmodel/tst_qitemselectionmodel.cpp @@ -103,6 +103,7 @@ private slots: void testValidRangesInSelectionsAfterReset(); void testChainedSelectionClear(); + void testClearCurrentIndex(); private: QAbstractItemModel *model; @@ -2681,6 +2682,12 @@ public: m_target->setCurrentIndex(index, command); } + void clearCurrentIndex() + { + QItemSelectionModel::clearCurrentIndex(); + m_target->clearCurrentIndex(); + } + private: QItemSelectionModel *m_target; @@ -2717,6 +2724,31 @@ void tst_QItemSelectionModel::testChainedSelectionClear() duplicate.setCurrentIndex(model.index(0, 0), QItemSelectionModel::NoUpdate); QVERIFY(selectionModel.currentIndex() == duplicate.currentIndex()); + + duplicate.clearCurrentIndex(); + + QVERIFY(!duplicate.currentIndex().isValid()); + QVERIFY(selectionModel.currentIndex() == duplicate.currentIndex()); +} + +void tst_QItemSelectionModel::testClearCurrentIndex() +{ + QStringListModel model(QStringList() << "Apples" << "Pears"); + + QItemSelectionModel selectionModel(&model, 0); + + QSignalSpy currentIndexSpy(&selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex))); + + QModelIndex firstIndex = model.index(0, 0); + QVERIFY(firstIndex.isValid()); + selectionModel.setCurrentIndex(firstIndex, QItemSelectionModel::NoUpdate); + QVERIFY(selectionModel.currentIndex() == firstIndex); + QVERIFY(currentIndexSpy.size() == 1); + + selectionModel.clearCurrentIndex(); + + QVERIFY(selectionModel.currentIndex() == QModelIndex()); + QVERIFY(currentIndexSpy.size() == 2); } QTEST_MAIN(tst_QItemSelectionModel) |