QAbtractItemView: deselect before click opens editor

A click on the selected item should deselect all other items before
editing starts.

Remove the part of the test case that assumes that we can have multiple
items selected in ExtendedSelection mode, and click on an item to start
editing while maintaining selection. That can never happen.

Fixes: QTBUG-111131
Change-Id: I0312eed4614502cfb77eca26d3f7615427493d7d
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 3f2d02e2f4436ace5f0fbbf432878a963f5ee969)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2023-02-26 16:47:42 +01:00 committed by Qt Cherry-pick Bot
parent acb0cb62fe
commit 0e5e7acca8
2 changed files with 14 additions and 17 deletions

View File

@ -1957,7 +1957,7 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
if (d->selectionModel && d->noSelectionOnMousePress) {
d->noSelectionOnMousePress = false;
if (!edited && !d->pressClosedEditor)
if (!d->pressClosedEditor)
d->selectionModel->select(index, selectionCommand(index, event));
}

View File

@ -755,22 +755,6 @@ void tst_QAbstractItemView::pressClosesReleaseDoesntOpenEditor()
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, inChildOutsideEditor); // should not reopen editor
QTest::qWait(QApplication::doubleClickInterval() * 2);
QCOMPARE(view.state(), QAbstractItemView::NoState);
// with multiple items selected, clicking from the currently edited item into another
// selected item closes the current and reopens a new editor
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
const QRect child2Rect = view.visualRect(model.indexFromItem(parent->child(1)));
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ControlModifier, child2Rect.center()); // select
QVERIFY(view.selectionModel()->selectedIndexes().contains(model.indexFromItem(parent->child(0))));
QVERIFY(view.selectionModel()->selectedIndexes().contains(model.indexFromItem(parent->child(1))));
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, child2Rect.center()); // edit
QTRY_COMPARE(view.state(), QAbstractItemView::EditingState);
QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, inChildOutsideEditor); // editor closes
QCOMPARE(view.state(), QAbstractItemView::NoState);
QTest::qWait(10); // process some events, let the internal timer time out
QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, inChildOutsideEditor); // should open editor
QTest::qWait(QApplication::doubleClickInterval() * 2);
QCOMPARE(view.state(), QAbstractItemView::EditingState);
}
@ -3044,6 +3028,19 @@ void tst_QAbstractItemView::mouseSelection_data()
<< QList{SelectionEvent(SelectionEvent::Click, 3),
SelectionEvent(SelectionEvent::Click, Qt::ControlModifier, 3)}
<< QList<int>{};
// Extended: when drag is enabled, click on selected without Ctrl clears before editing
QTest::addRow("Extended:Range,Click,editable") << QAbstractItemView::ExtendedSelection << false
<< QAbstractItemView::SelectedClicked
<< QList{SelectionEvent(SelectionEvent::Click, 1),
SelectionEvent(SelectionEvent::Click, Qt::ShiftModifier, 3),
SelectionEvent(SelectionEvent::Click, 2)}
<< QList<int>{2};
QTest::addRow("Extended:Range,Click,dragable,editable") << QAbstractItemView::ExtendedSelection << true
<< QAbstractItemView::SelectedClicked
<< QList{SelectionEvent(SelectionEvent::Click, 1),
SelectionEvent(SelectionEvent::Click, Qt::ShiftModifier, 3),
SelectionEvent(SelectionEvent::Click, 2)}
<< QList<int>{2};
}
void tst_QAbstractItemView::mouseSelection()