diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 59aaec1e9d..1df8f538a8 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -725,8 +725,8 @@ But the application can call \l edit() and \l closeEditor() manually. \value TableView.SingleTapped - the user can edit a cell by single tapping it. \value TableView.DoubleTapped - the user can edit a cell by double tapping it. - \value TableView.SelectedTapped - the user can edit the - \l {QItemSelectionModel::currentIndex()}{current cell} by tapping it. + \value TableView.SelectedTapped - the user can edit a + \l {QItemSelectionModel::selectedIndexes()}{selected cell} by tapping it. \value TableView.EditKeyPressed - the user can edit the \l {QItemSelectionModel::currentIndex()}{current cell} by pressing one of the edit keys. The edit keys are decided by the OS, but are normally @@ -4790,24 +4790,29 @@ void QQuickTableViewPrivate::handleTap(const QQuickHandlerPoint &point) if (resizeHandler->state() != QQuickTableViewResizeHandler::Listening) return; - QModelIndex prevIndex; - if (selectionModel) { - prevIndex = selectionModel->currentIndex(); - if (pointerNavigationEnabled) { + const QModelIndex tappedIndex = q->modelIndex(q->cellAtPosition(point.position())); + bool tappedCellIsSelected = false; + + if (selectionModel) + tappedCellIsSelected = selectionModel->isSelected(tappedIndex); + + if (canEdit(tappedIndex, false)) { + if (editTriggers & QQuickTableView::SingleTapped) { clearSelection(); - setCurrentIndexFromTap(point.position()); + q->edit(tappedIndex); + return; + } else if (editTriggers & QQuickTableView::SelectedTapped && tappedCellIsSelected) { + q->edit(tappedIndex); + return; } } - if (editTriggers != QQuickTableView::NoEditTriggers) + // Since the tap didn't result in selecting or editing cells, we clear + // the current selection and move the current index instead. + if (pointerNavigationEnabled) { q->closeEditor(); - - const QModelIndex tappedIndex = q->modelIndex(q->cellAtPosition(point.position())); - if (canEdit(tappedIndex, false)) { - if (editTriggers & QQuickTableView::SingleTapped) - q->edit(tappedIndex); - else if ((editTriggers & QQuickTableView::SelectedTapped) && tappedIndex == prevIndex) - q->edit(tappedIndex); + clearSelection(); + setCurrentIndexFromTap(point.position()); } } diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index dc8b480cc3..0152b47316 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -6591,7 +6591,7 @@ void tst_QQuickTableView::editUsingEditTriggers() if (editTriggers & QQuickTableView::SelectedTapped) { // select cell first, then tap on it - tableView->selectionModel()->setCurrentIndex(index1, QItemSelectionModel::NoUpdate); + tableView->selectionModel()->setCurrentIndex(index1, QItemSelectionModel::Select); QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, tapPos1); QCOMPARE(tableView->selectionModel()->currentIndex(), index1); const auto editItem1 = tableView->property(kEditItem).value(); @@ -6612,6 +6612,11 @@ void tst_QQuickTableView::editUsingEditTriggers() QVERIFY(!tableView->property(kEditItem).value()); QVERIFY(!tableView->property(kEditIndex).value().isValid()); QCOMPARE(tableView->selectionModel()->currentIndex(), index2); + + // tap on the current cell. This alone should not start an edit (unless it's also selected) + QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, tapPos1); + QVERIFY(!tableView->property(kEditItem).value()); + QVERIFY(!tableView->property(kEditIndex).value().isValid()); } if (editTriggers & QQuickTableView::EditKeyPressed) {