QWidgetWindow: send QContextMenuEvent even after accepted mouse press

It would be more consistent if we could rely on the accepted state of
the original QMouseEvent to decide whether to follow up with a
QContextMenuEvent; but not all Qt widgets call ignore() on unhandled
mouse events, both in Qt and in external libraries and applications
(including some from KDE). So we should at least wait until Qt 7 to
make this a requirement. It seems sensible to move the check into
QWindow::event() rather than trying to distinguish the window type in
maybeSynthesizeContextMenuEvent(). We merely output a categorized log
message to indicate when the legacy behavior is in effect.

Amends 84a5f50c7766c99f62b22bb4388137e0aa8dd13d

[ChangeLog][QtWidgets] If your QWidget subclass depends on receiving
QContextMenuEvent, and also handles mouse events directly, we
recommend that you call ignore() on unhandled mouse events (such as
right-button events). In Qt 7, we plan to stop sending
QContextMenuEvent if the triggering mouse event is accepted.

Fixes: QTBUG-132066
Change-Id: I454813dab4c387112f161fc28a0ee94570013afa
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
(cherry picked from commit 357c64a99607456133bfabf86d6b67162717cb29)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Shawn Rutledge 2024-12-09 22:16:37 +01:00 committed by Qt Cherry-pick Bot
parent 75cb14deb8
commit f329dd4e16
2 changed files with 24 additions and 12 deletions

View File

@ -2618,6 +2618,7 @@ bool QWindow::event(QEvent *ev)
case QEvent::MouseButtonPress: { case QEvent::MouseButtonPress: {
auto *me = static_cast<QMouseEvent*>(ev); auto *me = static_cast<QMouseEvent*>(ev);
mousePressEvent(me); mousePressEvent(me);
if (!ev->isAccepted())
d->maybeSynthesizeContextMenuEvent(me); d->maybeSynthesizeContextMenuEvent(me);
break; break;
} }
@ -2625,6 +2626,7 @@ bool QWindow::event(QEvent *ev)
case QEvent::MouseButtonRelease: { case QEvent::MouseButtonRelease: {
auto *me = static_cast<QMouseEvent*>(ev); auto *me = static_cast<QMouseEvent*>(ev);
mouseReleaseEvent(me); mouseReleaseEvent(me);
if (!ev->isAccepted())
d->maybeSynthesizeContextMenuEvent(me); d->maybeSynthesizeContextMenuEvent(me);
break; break;
} }
@ -2748,18 +2750,21 @@ bool QWindow::event(QEvent *ev)
} }
/*! \internal /*! \internal
Synthesize and send a QContextMenuEvent if the given \a event is a suitable Synthesize and send a QContextMenuEvent if the given \a event is a
mouse event (a right-button press or release, depending on suitable mouse event (a right-button press or release, depending on
QStyleHints::contextMenuTrigger()) that was *not accepted* and *isn't* QStyleHints::contextMenuTrigger()) that *isn't* exclusively grabbed.
exclusively grabbed. On most platforms, it's done on mouse release; on On most platforms, it's done on mouse release; on
Windows, it's done on press, because of the potential to support Windows, it's done on press, because of the potential to support
right-button clicks and drags to select or lasso items, and then still right-button clicks and drags to select or lasso items, and then still
getting a context menu at the end of that gesture. (That is in conflict getting a context menu at the end of that gesture. (That is in conflict
with supporting the press-drag-release gesture to select menu items on the with supporting the press-drag-release gesture to select menu items on the
context menus themselves. Context menus can be implemented that way by context menus themselves. Context menus can be implemented that way by
handling the separate press, move and release events.) Any time the handling the separate press, move and release events.) Any time the
\a event was already handled in some way, it must be accepted, to avoid \a event was already handled in some way, it *should* be accepted, to
synthesis of the QContextMenuEvent here. indicate that it's not necessary to synthesize a QContextMenuEvent here.
However, there's enough legacy widget code that doesn't call ignore()
on unhandled mouse events, that in QWidgetWindow, we put off requiring
the event to be ignored. Hopefully we can begin requiring it in Qt 7.
The QContextMenuEvent occurs at the scenePosition(). The position() The QContextMenuEvent occurs at the scenePosition(). The position()
was likely already "localized" during the previous delivery. was likely already "localized" during the previous delivery.
@ -2779,12 +2784,13 @@ bool QWindow::event(QEvent *ev)
void QWindowPrivate::maybeSynthesizeContextMenuEvent(QMouseEvent *event) void QWindowPrivate::maybeSynthesizeContextMenuEvent(QMouseEvent *event)
{ {
#ifndef QT_NO_CONTEXTMENU #ifndef QT_NO_CONTEXTMENU
if (!event->isAccepted() && !event->exclusivePointGrabber() if (!event->exclusivePointGrabber() && event->button() == Qt::RightButton
&& event->button() == Qt::RightButton
&& event->type() == QGuiApplicationPrivate::contextMenuEventType()) { && event->type() == QGuiApplicationPrivate::contextMenuEventType()) {
QContextMenuEvent e(QContextMenuEvent::Mouse, event->scenePosition().toPoint(), QContextMenuEvent e(QContextMenuEvent::Mouse, event->scenePosition().toPoint(),
event->globalPosition().toPoint(), event->modifiers()); event->globalPosition().toPoint(), event->modifiers());
qCDebug(lcPopup) << "synthesized QContextMenuEvent after ignored" << event->type() << ":" << &e; qCDebug(lcPopup) << "synthesized after"
<< (event->isAccepted() ? "ACCEPTED (legacy behavior)" : "ignored")
<< event->type() << ":" << &e;
QGuiApplication::sendEvent(q_func(), &e); QGuiApplication::sendEvent(q_func(), &e);
} }
#endif #endif

View File

@ -666,6 +666,12 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event)
event->setAccepted(translated.isAccepted()); event->setAccepted(translated.isAccepted());
} }
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
if (
#else
if (event->isAccepted() &&
#endif
(event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease))
d->maybeSynthesizeContextMenuEvent(event); d->maybeSynthesizeContextMenuEvent(event);
} }