diff options
-rw-r--r-- | src/gui/itemviews/qabstractitemview.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qlistview/tst_qlistview.cpp | 30 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/gui/itemviews/qabstractitemview.cpp b/src/gui/itemviews/qabstractitemview.cpp index 52529ff..99e9aeb 100644 --- a/src/gui/itemviews/qabstractitemview.cpp +++ b/src/gui/itemviews/qabstractitemview.cpp @@ -1661,6 +1661,10 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event) EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers); bool edited = edit(index, trigger, event); + //in the case the user presses on no item we might decide to clear the selection + if (d->selectionModel && !index.isValid()) + d->selectionModel->select(QModelIndex(), selectionCommand(index, event)); + setState(NoState); if (click) { diff --git a/tests/auto/qlistview/tst_qlistview.cpp b/tests/auto/qlistview/tst_qlistview.cpp index 2be1a03..2831747 100644 --- a/tests/auto/qlistview/tst_qlistview.cpp +++ b/tests/auto/qlistview/tst_qlistview.cpp @@ -113,6 +113,7 @@ private slots: void task254449_draggingItemToNegativeCoordinates(); void keyboardSearch(); void shiftSelectionWithNonUniformItemSizes(); + void clickOnViewportClearsSelection(); }; // Testing get/set functions @@ -1733,5 +1734,34 @@ void tst_QListView::shiftSelectionWithNonUniformItemSizes() } } +void tst_QListView::clickOnViewportClearsSelection() +{ + QStringList items; + items << "Text1"; + QStringListModel model(items); + QListView view; + view.setModel(&model); + view.setSelectionMode(QListView::ExtendedSelection); + + view.selectAll(); + QModelIndex index = model.index(0); + QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1); + QVERIFY(view.selectionModel()->isSelected(index)); + + //we try to click outside of the index + const QPoint point = view.visualRect(index).bottomRight() + QPoint(10,10); + + QTest::mousePress(view.viewport(), Qt::LeftButton, 0, point); + //at this point, the selection shouldn't have changed + QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1); + QVERIFY(view.selectionModel()->isSelected(index)); + + QTest::mouseRelease(view.viewport(), Qt::LeftButton, 0, point); + //now the selection should be cleared + QVERIFY(!view.selectionModel()->hasSelection()); + +} + + QTEST_MAIN(tst_QListView) #include "tst_qlistview.moc" |