summaryrefslogtreecommitdiffstats
path: root/src
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 /src
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 'src')
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp17
-rw-r--r--src/widgets/itemviews/qabstractitemview_p.h3
2 files changed, 18 insertions, 2 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index cfb6a3151a..5afdd93b34 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -86,6 +86,7 @@ QAbstractItemViewPrivate::QAbstractItemViewPrivate()
selectionMode(QAbstractItemView::ExtendedSelection),
selectionBehavior(QAbstractItemView::SelectItems),
currentlyCommittingEditor(nullptr),
+ pressClosedEditor(false),
pressedModifiers(Qt::NoModifier),
pressedPosition(QPoint(-1, -1)),
pressedAlreadySelected(false),
@@ -1777,6 +1778,9 @@ void QAbstractItemView::mousePressEvent(QMouseEvent *event)
QPoint pos = event->position().toPoint();
QPersistentModelIndex index = indexAt(pos);
+ // this is the mouse press event that closed the last editor (via focus event)
+ d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
+
if (!d->selectionModel
|| (d->state == EditingState && d->hasEditor(index)))
return;
@@ -1935,16 +1939,17 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
bool selectedClicked = click && (event->button() == Qt::LeftButton) && d->pressedAlreadySelected;
EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
- const bool edited = click ? edit(index, trigger, event) : false;
+ const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) : false;
d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
if (d->selectionModel && d->noSelectionOnMousePress) {
d->noSelectionOnMousePress = false;
- if (!edited)
+ if (!edited && !d->pressClosedEditor)
d->selectionModel->select(index, selectionCommand(index, event));
}
+ d->pressClosedEditor = false;
setState(NoState);
if (click) {
@@ -2584,6 +2589,8 @@ void QAbstractItemView::timerEvent(QTimerEvent *event)
//we only get here if there was no double click
if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
scrollTo(d->pressedIndex);
+ } else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
+ d->pressClosedEditorWatcher.stop();
}
}
@@ -2854,6 +2861,12 @@ void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndE
if (!index.isValid())
return; // the editor was not registered
+ // start a timer that expires immediately when we return to the event loop
+ // to identify whether this close was triggered by a mousepress-initiated
+ // focus event
+ d->pressClosedEditorWatcher.start(0, this);
+ d->lastEditedIndex = index;
+
if (!isPersistent) {
setState(NoState);
QModelIndex index = d->indexForEditor(editor);
diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h
index 521d341b8f..f45d642dcf 100644
--- a/src/widgets/itemviews/qabstractitemview_p.h
+++ b/src/widgets/itemviews/qabstractitemview_p.h
@@ -364,6 +364,9 @@ public:
QIndexEditorHash indexEditorHash;
QSet<QWidget*> persistent;
QWidget *currentlyCommittingEditor;
+ QBasicTimer pressClosedEditorWatcher;
+ QPersistentModelIndex lastEditedIndex;
+ bool pressClosedEditor;
QPersistentModelIndex enteredIndex;
QPersistentModelIndex pressedIndex;