summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/itemviews
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2021-06-07 09:40:12 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2021-06-14 13:01:52 +0000
commit2f9543c2ef71e3b5b686ce07e84fd4372ce0535f (patch)
tree8d989475a0864f3b729cfa7f243940b38ed26566 /tests/auto/widgets/itemviews
parentb43ec7e9f9d043ee3b82a5207ac124d77ce5afca (diff)
QAIV: Don't open editor on release when press closed editor
A mouse press that transfers focus from an open editor back to the view will close the editor. To prevent that the corresponding release then opens the same editor again we need to know that the closeEditor call was caused by the mouse press. Since Qt first generates the focusOut event, and then delivers the mouse press, we have to start a zero-timer to check whether we are in the same event delivery process. If so, ignore the corresponding release. Add test case that simulates that chain of events. Fixes: QTBUG-20456 Pick-to: 6.2 6.1 Change-Id: I28fa32bfbc776db207c594c329961f575ae58ea9 Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Diffstat (limited to 'tests/auto/widgets/itemviews')
-rw-r--r--tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
index e5b24b1a9b..7c4dc38d6d 100644
--- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
+++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
@@ -102,6 +102,7 @@ private slots:
void selectAll();
void ctrlA();
void persistentEditorFocus();
+ void pressClosesReleaseDoesntOpenEditor();
void setItemDelegate();
void setItemDelegate_data();
// The dragAndDrop() test doesn't work, and is thus disabled on Mac and Windows
@@ -732,6 +733,63 @@ void tst_QAbstractItemView::persistentEditorFocus()
}
}
+/*!
+ A press into the selection area of an item being edited, but outside the editor,
+ closes the editor by transferring focus to the view. The corresponding release
+ should then not re-open the editor.
+
+ QTBUG-20456.
+*/
+void tst_QAbstractItemView::pressClosesReleaseDoesntOpenEditor()
+{
+ QStandardItemModel model(0, 1);
+ auto *parent = new QStandardItem("parent");
+ for (const auto &childText : {"child1", "child2"}) {
+ auto *child = new QStandardItem(childText);
+ child->setFlags(Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsEditable | Qt::ItemIsSelectable);
+ parent->appendRow(child);
+ }
+ model.appendRow(parent);
+
+ QTreeView view;
+ view.setModel(&model);
+ view.setExpanded(model.indexFromItem(parent), true);
+ view.setSelectionMode(QAbstractItemView::SingleSelection);
+ view.setEditTriggers(QAbstractItemView::SelectedClicked | QAbstractItemView::DoubleClicked);
+
+ view.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&view));
+
+ const QRect childRect = view.visualRect(model.indexFromItem(parent->child(0)));
+ QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, childRect.center()); // select
+ QVERIFY(view.selectionModel()->selectedIndexes().contains(model.indexFromItem(parent->child(0))));
+ QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, childRect.center()); // edit
+ QTRY_COMPARE(view.state(), QAbstractItemView::EditingState);
+ QPoint inChildOutsideEditor = QPoint(view.indentation() / 2, childRect.center().y());
+ QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, inChildOutsideEditor); // focus itemview, 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 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);
+}
+
#if !defined(Q_OS_MAC) && !defined(Q_OS_WIN)