diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index 2b2f130060..6cbf51efd9 100644 --- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp +++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp @@ -253,6 +253,7 @@ private slots: void shiftSelectionAfterChangingModelContents(); void QTBUG48968_reentrant_updateEditorGeometries(); void QTBUG50535_update_on_new_selection_model(); + void testSelectionModelInSyncWithView(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -2082,5 +2083,59 @@ void tst_QAbstractItemView::QTBUG50535_update_on_new_selection_model() QTRY_VERIFY(view.m_paintEventsCount > oldPaintEventsCount); } +void tst_QAbstractItemView::testSelectionModelInSyncWithView() +{ + QStandardItemModel model; + for (int i = 0; i < 10; ++i) + model.appendRow(new QStandardItem(QStringLiteral("%1").arg(i))); + + class ListView : public QListView + { + public: + using QListView::selectedIndexes; + }; + + ListView view; + QVERIFY(!view.selectionModel()); + + view.setModel(&model); + QVERIFY(view.selectionModel()); + QVERIFY(view.selectedIndexes().isEmpty()); + QVERIFY(view.selectionModel()->selection().isEmpty()); + + view.setCurrentIndex(model.index(0, 0)); + QCOMPARE(view.currentIndex(), model.index(0, 0)); + QCOMPARE(view.selectionModel()->currentIndex(), model.index(0, 0)); + + view.selectionModel()->setCurrentIndex(model.index(1, 0), QItemSelectionModel::SelectCurrent); + QCOMPARE(view.currentIndex(), model.index(1, 0)); + QCOMPARE(view.selectedIndexes(), QModelIndexList() << model.index(1, 0)); + QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0)); + QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(1, 0)); + + view.show(); + QVERIFY(QTest::qWaitForWindowExposed(&view)); + + QItemSelectionModel selectionModel(&model); + selectionModel.setCurrentIndex(model.index(2, 0), QItemSelectionModel::Current); + + view.setSelectionModel(&selectionModel); + QCOMPARE(view.currentIndex(), model.index(2, 0)); + QCOMPARE(view.selectedIndexes(), QModelIndexList()); + QCOMPARE(view.selectionModel()->currentIndex(), model.index(2, 0)); + QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList()); + + + QItemSelectionModel selectionModel2(&model); + selectionModel2.select(model.index(0, 0), QItemSelectionModel::ClearAndSelect); + selectionModel2.setCurrentIndex(model.index(1, 0), QItemSelectionModel::Current); + + view.setSelectionModel(&selectionModel2); + QCOMPARE(view.currentIndex(), model.index(1, 0)); + QCOMPARE(view.selectedIndexes(), QModelIndexList() << model.index(0, 0)); + QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0)); + QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(0, 0)); +} + QTEST_MAIN(tst_QAbstractItemView) #include "tst_qabstractitemview.moc"