QQuickTableView: only clear selections when selections are enabled

TableView should only modify a selection (which includes clearing
it) when selections are enabled. Otherwise it should leave it to
the application to modify the selection model explicitly.

This can be used to set a selection that "sticks", regardless of
what the user does.

Change-Id: I264946d1b3513846794e1fe12b7d94cfaef050ed
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
(cherry picked from commit 9b804c58fc)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Richard Moe Gustavsen 2023-03-15 10:26:44 +01:00 committed by Qt Cherry-pick Bot
parent 6fea78bca7
commit bf7881dddf
2 changed files with 21 additions and 4 deletions

View File

@ -4802,7 +4802,8 @@ void QQuickTableViewPrivate::handleTap(const QQuickHandlerPoint &point)
if (canEdit(tappedIndex, false)) {
if (editTriggers & QQuickTableView::SingleTapped) {
clearSelection();
if (selectionBehavior != QQuickTableView::SelectionDisabled)
clearSelection();
q->edit(tappedIndex);
return;
} else if (editTriggers & QQuickTableView::SelectedTapped && tappedCellIsSelected) {
@ -4815,7 +4816,8 @@ void QQuickTableViewPrivate::handleTap(const QQuickHandlerPoint &point)
// the current selection and move the current index instead.
if (pointerNavigationEnabled) {
q->closeEditor();
clearSelection();
if (selectionBehavior != QQuickTableView::SelectionDisabled)
clearSelection();
setCurrentIndexFromTap(point.position());
}
}

View File

@ -208,6 +208,7 @@ private slots:
void testSelectableStartPosEndPosOutsideView();
void testSelectableScrollTowardsPos();
void setCurrentIndexFromSelectionModel();
void clearSelectionOnTap_data();
void clearSelectionOnTap();
void moveCurrentIndexUsingArrowKeys();
void moveCurrentIndexUsingHomeAndEndKeys();
@ -4744,12 +4745,25 @@ void tst_QQuickTableView::setCurrentIndexFromSelectionModel()
QVERIFY(tableView->itemAtCell(cellAtEnd)->property(kCurrent).toBool());
}
void tst_QQuickTableView::clearSelectionOnTap_data()
{
QTest::addColumn<bool>("selectionEnabled");
QTest::newRow("selections enabled") << true;
QTest::newRow("selections disabled") << false;
}
void tst_QQuickTableView::clearSelectionOnTap()
{
// Check that we clear the current selection when tapping
// inside TableView. But only if TableView has selections
// enabled. Otherwise, TableView should not touch the selection model.
QFETCH(bool, selectionEnabled);
LOAD_TABLEVIEW("tableviewwithselected2.qml");
TestModel model(40, 40);
tableView->setModel(QVariant::fromValue(&model));
if (!selectionEnabled)
tableView->setSelectionBehavior(QQuickTableView::SelectionDisabled);
WAIT_UNTIL_POLISHED;
@ -4758,13 +4772,14 @@ void tst_QQuickTableView::clearSelectionOnTap()
tableView->selectionModel()->select(index, QItemSelectionModel::Select);
QCOMPARE(tableView->selectionModel()->selectedIndexes().size(), 1);
// Click on a cell. This should remove the selection
// Click on a cell
const auto item = tableView->itemAtIndex(tableView->index(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().size(), 0);
QCOMPARE(tableView->selectionModel()->hasSelection(), !selectionEnabled);
}
void tst_QQuickTableView::moveCurrentIndexUsingArrowKeys()