From 04d75342dd57b033036484f212b7b6a1577b6530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Arve=20S=C3=A6ther?= Date: Mon, 23 Dec 2024 12:41:00 +0100 Subject: [PATCH] Fix propagation of QContextMenuEvent for QQuickItems QQuickDeliveryAgentPrivate::deliverContextMenuEvent() relies on that the QContextMenuEvent gets ignored if the target item doesn't handle it, therefore we have to make sure it is ignored by default. This fixes the context menu for the SideBar (still WIP) and likewise the autotest in this change. Pick-to: 6.9 Change-Id: If72a814adc92aea6e5e071eb49cbe84b025768fb Reviewed-by: Shawn Rutledge --- src/quick/items/qquickitem.cpp | 2 + .../customContextMenuOnNestedRectangle.qml | 39 +++++++++++++++++++ .../tst_qquickcontextmenu.cpp | 1 + 3 files changed, 42 insertions(+) create mode 100644 tests/auto/quickcontrols/qquickcontextmenu/data/customContextMenuOnNestedRectangle.qml diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index db40b80c4d..6ca7eb52cf 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -9184,6 +9184,8 @@ bool QQuickItem::event(QEvent *ev) break; case QEvent::ContextMenu: // ### Qt 7: add virtual contextMenuEvent (and to QWindow?) + ev->ignore(); + break; default: return QObject::event(ev); } diff --git a/tests/auto/quickcontrols/qquickcontextmenu/data/customContextMenuOnNestedRectangle.qml b/tests/auto/quickcontrols/qquickcontextmenu/data/customContextMenuOnNestedRectangle.qml new file mode 100644 index 0000000000..2027707765 --- /dev/null +++ b/tests/auto/quickcontrols/qquickcontextmenu/data/customContextMenuOnNestedRectangle.qml @@ -0,0 +1,39 @@ +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + width: 600 + height: 400 + //visible: true // for manual testing + + Rectangle { + objectName: "tomato" + x: 100 + y: 100 + width: 100 + height: 100 + color: "tomato" + radius: width / 2 + + ContextMenu.menu: Menu { + id: contextMenu + MenuItem { + text: qsTr("Eat tomato") + } + MenuItem { + text: qsTr("Throw tomato") + } + MenuItem { + text: qsTr("Squash tomato") + } + } + Rectangle { + objectName: "orange" + anchors.centerIn: parent + width: 80 + height: 80 + color: "orange" + radius: width / 2 + } + } +} diff --git a/tests/auto/quickcontrols/qquickcontextmenu/tst_qquickcontextmenu.cpp b/tests/auto/quickcontrols/qquickcontextmenu/tst_qquickcontextmenu.cpp index 4c42fb39a0..7a49a3dc61 100644 --- a/tests/auto/quickcontrols/qquickcontextmenu/tst_qquickcontextmenu.cpp +++ b/tests/auto/quickcontrols/qquickcontextmenu/tst_qquickcontextmenu.cpp @@ -47,6 +47,7 @@ void tst_QQuickContextMenu::customContextMenu_data() QTest::addRow("Rectangle") << "customContextMenuOnRectangle.qml"; QTest::addRow("Control") << "customContextMenuOnControl.qml"; + QTest::addRow("NestedRectangle") << "customContextMenuOnNestedRectangle.qml"; } void tst_QQuickContextMenu::customContextMenu()