aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-08-10 14:13:52 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-11 22:45:54 +0000
commit8ca41ef6e23ce9a3f8ef04069a4d0f6e34b3114b (patch)
treea27225f535919fe701149aa2cc6b4a00edd1f4fe
parent7c5570ca8c010b2f8fecd2073dd039e665b83804 (diff)
QQuickTableView: clear selection on tap directly in TableView
Before 6.4, it was only possible to make a selection in TableView by using a SelectionRectangle. But in 6.4, you can also make a selection directly in TableView by using the shift+arrow keys. To make sure that a selection is cleared either way on tap, move the implementation that clears the selection from SelectionRectangle to TableView. Change-Id: Ic718763bf9b643cb3e3c04cc3b90c6ffcf9ca4b2 Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io> (cherry picked from commit b51ad14bc6a055de22b9c5f0083a3ca42795779b) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/quick/items/qquicktableview.cpp12
-rw-r--r--src/quicktemplates2/qquickselectionrectangle.cpp1
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp24
3 files changed, 32 insertions, 5 deletions
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 856a8d7794..eeaf95068f 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");