diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index a8320d99b2..54781968b4 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -4039,15 +4039,19 @@ void QQuickTableViewPrivate::init() positionYAnimation.stop(); if (keyNavigationEnabled) q->forceActiveFocus(Qt::MouseFocusReason); - if (!q->isInteractive()) - setCurrentIndexFromTap(tapHandler->point().pressPosition()); + if (q->isInteractive()) + return; + clearSelection(); + setCurrentIndexFromTap(tapHandler->point().pressPosition()); }); QObject::connect(tapHandler, &QQuickTapHandler::tapped, [this, q, tapHandler] { if (!pointerNavigationEnabled) return; - if (q->isInteractive()) - setCurrentIndexFromTap(tapHandler->point().pressPosition()); + if (!q->isInteractive()) + return; + clearSelection(); + setCurrentIndexFromTap(tapHandler->point().pressPosition()); }); } diff --git a/src/quicktemplates2/qquickselectionrectangle.cpp b/src/quicktemplates2/qquickselectionrectangle.cpp index 0e38415a06..1730fec8f2 100644 --- a/src/quicktemplates2/qquickselectionrectangle.cpp +++ b/src/quicktemplates2/qquickselectionrectangle.cpp @@ -145,7 +145,6 @@ QQuickSelectionRectanglePrivate::QQuickSelectionRectanglePrivate() }); QObject::connect(m_tapHandler, &QQuickTapHandler::tapped, [this] { - m_selectable->clearSelection(); updateActiveState(false); }); diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index 0455a16070..22dbfc08e1 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -199,6 +199,7 @@ private slots: void testSelectableStartPosEndPosOutsideView(); void testSelectableScrollTowardsPos(); void setCurrentIndexFromSelectionModel(); + void clearSelectionOnTap(); void moveCurrentIndexUsingArrowKeys(); void moveCurrentIndexUsingHomeAndEndKeys(); void moveCurrentIndexUsingPageUpDownKeys(); @@ -4443,6 +4444,29 @@ void tst_QQuickTableView::setCurrentIndexFromSelectionModel() QVERIFY(tableView->itemAtCell(cellAtEnd)->property(kCurrent).toBool()); } +void tst_QQuickTableView::clearSelectionOnTap() +{ + LOAD_TABLEVIEW("tableviewwithselected2.qml"); + + TestModel model(40, 40); + tableView->setModel(QVariant::fromValue(&model)); + + WAIT_UNTIL_POLISHED; + + // Select root item + const auto index = tableView->selectionModel()->model()->index(0, 0); + tableView->selectionModel()->select(index, QItemSelectionModel::Select); + QCOMPARE(tableView->selectionModel()->selectedIndexes().count(), 1); + + // Click on a cell. This should remove the selection + const auto item = tableView->itemAtCell(0, 0); + QVERIFY(item); + QPoint localPos = QPoint(item->width() / 2, item->height() / 2); + QPoint pos = item->window()->contentItem()->mapFromItem(item, localPos).toPoint(); + QTest::mouseClick(item->window(), Qt::LeftButton, Qt::NoModifier, pos); + QCOMPARE(tableView->selectionModel()->selectedIndexes().count(), 0); +} + void tst_QQuickTableView::moveCurrentIndexUsingArrowKeys() { LOAD_TABLEVIEW("tableviewwithselected1.qml");