Restore event replay when a popup is closed by mouse press outside

If a popup (such as a context menu or combobox) is open, and the user
presses any mouse button outside the popup, Windows users expect the
mouse event to be sent to whatever is under the mouse, while the popup
closes. (So the popup must close on press, not on release.)
QPlatformIntegration::ReplayMousePressOutsidePopup requests this
platform-specific behavior in general, and the WA_NoMouseReplay
attribute can disable it on specific widgets.

e4ef0f03e6f1fddc397980fd7fbf6f6b829f16d9 removed this feature which was
added to Qt 5 in 1f456b4cbb93e3fea699878d117900b703146213, based on
doubt that we really needed it: and if we did, maybe we would need it in
QtGui. But so far it seems the main excuse for doing it this way is that
popups are sometimes opened with exec(). If the nested event loop
handles the mouse press completely, and the QPA event is discarded, the
outer loop has no chance of seeing it after exec() finishes.
In Qt Quick, we don't use exec(); so let's assume that this continues to
be needed only for widgets.

At least we don't use extern sharing of a global bool in this version.

Fixes: QTBUG-130138
Change-Id: I95b5d24ee9bc8608655ed5c585d1d91a891fbad3
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 601924cce57ee564ec149a170f1987b13924e0ee)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Shawn Rutledge 2024-11-01 12:31:56 +01:00 committed by Qt Cherry-pick Bot
parent d480177789
commit 3544fa163f
3 changed files with 44 additions and 0 deletions

View File

@ -124,6 +124,8 @@ QApplicationPrivate *QApplicationPrivate::self = nullptr;
bool QApplicationPrivate::autoSipEnabled = true;
bool QApplicationPrivate::replayMousePress = false;
QApplicationPrivate::QApplicationPrivate(int &argc, char **argv)
: QGuiApplicationPrivate(argc, argv)
{
@ -3350,6 +3352,12 @@ void QApplicationPrivate::closePopup(QWidget *popup)
if (popupGrabOk) {
popupGrabOk = false;
if (active_window && active_window->windowHandle()
&& !popup->geometry().contains(QGuiApplicationPrivate::lastCursorPosition.toPoint())
&& !popup->testAttribute(Qt::WA_NoMouseReplay)) {
QApplicationPrivate::replayMousePress = true;
}
// transfer grab back to mouse grabber if any, otherwise release the grab
ungrabMouseForPopup(popup);

View File

@ -109,6 +109,8 @@ public:
static bool inPopupMode();
void closePopup(QWidget *popup);
void openPopup(QWidget *popup);
static bool replayMousePress;
static void setFocusWidget(QWidget *focus, Qt::FocusReason reason);
static QWidget *focusNextPrevChild_helper(QWidget *toplevel, bool next,
bool *wrappingOccurred = nullptr);

View File

@ -588,6 +588,40 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event)
}
}
if (QApplication::activePopupWidget() != activePopupWidget
&& QApplicationPrivate::replayMousePress
&& QGuiApplicationPrivate::platformIntegration()->styleHint(QPlatformIntegration::ReplayMousePressOutsidePopup).toBool()) {
if (m_widget->windowType() != Qt::Popup)
qt_button_down = nullptr;
if (event->type() == QEvent::MouseButtonPress) {
// the popup disappeared: replay the mouse press event to whatever is behind it
QWidget *w = QApplication::widgetAt(event->globalPosition().toPoint());
if (w && !QApplicationPrivate::isBlockedByModal(w)) {
// activate window of the widget under mouse pointer
if (!w->isActiveWindow()) {
w->activateWindow();
w->window()->raise();
}
if (auto win = qt_widget_private(w)->windowHandle(QWidgetPrivate::WindowHandleMode::Closest)) {
const QRect globalGeometry = win->isTopLevel()
? win->geometry()
: QRect(win->mapToGlobal(QPoint(0, 0)), win->size());
if (globalGeometry.contains(event->globalPosition().toPoint())) {
// Use postEvent() to ensure the local QEventLoop terminates when called from QMenu::exec()
const QPoint localPos = win->mapFromGlobal(event->globalPosition().toPoint());
QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress, localPos, localPos, event->globalPosition().toPoint(),
event->button(), event->buttons(), event->modifiers(), event->source());
QCoreApplicationPrivate::setEventSpontaneous(e, true);
e->setTimestamp(event->timestamp());
QCoreApplication::postEvent(win, e);
}
}
}
}
QApplicationPrivate::replayMousePress = false;
}
if (releaseAfter) {
qt_button_down = nullptr;
qt_popup_down_closed = false;