aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2025-05-13 03:40:58 +0000
committerMitch Curtis <mitch.curtis@qt.io>2025-05-19 18:16:10 +0800
commit3461a35497d67a8b942f0c33879afd81dc9f3b2c (patch)
tree24e429000f7b11c66c190135b26b5533bb8c3a84
parent6dbd71c3df18badad640960e9429383183408d89 (diff)
Revert "Close all other Menus when showing non-sub-menu"
This reverts commit 4fc898246aa96922a28d5206c79ad00dc040d930. Reason for revert: dfcde3ea4d8a81d511cce5fb5eccdbb47e92d2c7, which this requires, causes a regression: QTBUG-134903. The documentation part of this patch that doesn't rely on the content added in dfcde3e will be re-introduced in a separate patch. Change-Id: Idb4d74fd963f6c0759b8a05b686f1a977d97013c Reviewed-by: Akseli Salovaara <akseli.salovaara@qt.io>
-rw-r--r--src/quickcontrols/doc/snippets/qtquickcontrols-menu-contextmenu.qml37
-rw-r--r--src/quickcontrols/doc/snippets/qtquickcontrols-menu-text-editing-contextmenu.qml41
-rw-r--r--src/quicktemplates/qquickmenu.cpp48
-rw-r--r--tests/auto/quickcontrols/qquicktextarea/data/customContextMenuOnRelease.qml44
-rw-r--r--tests/auto/quickcontrols/qquicktextarea/tst_qquicktextarea.cpp37
5 files changed, 22 insertions, 185 deletions
diff --git a/src/quickcontrols/doc/snippets/qtquickcontrols-menu-contextmenu.qml b/src/quickcontrols/doc/snippets/qtquickcontrols-menu-contextmenu.qml
index b023c87a7d..70c0d0c849 100644
--- a/src/quickcontrols/doc/snippets/qtquickcontrols-menu-contextmenu.qml
+++ b/src/quickcontrols/doc/snippets/qtquickcontrols-menu-contextmenu.qml
@@ -4,33 +4,24 @@
import QtQuick
import QtQuick.Controls
-Item {
-//! [children]
- TapHandler {
- acceptedButtons: Qt.RightButton
- onPressedChanged: {
- if (pressed && Application.styleHints.contextMenuTrigger === Qt.ContextMenuTrigger.Press)
- contextMenu.popup()
- }
- onTapped: {
- if (Application.styleHints.contextMenuTrigger === Qt.ContextMenuTrigger.Release)
- contextMenu.popup()
- }
+//! [root]
+MouseArea {
+ anchors.fill: parent
+ acceptedButtons: Qt.LeftButton | Qt.RightButton
+ onClicked: (mouse) => {
+ if (mouse.button === Qt.RightButton)
+ contextMenu.popup()
}
- TapHandler {
- acceptedDevices: PointerDevice.TouchScreen
- onLongPressed: contextMenu.popup()
+ onPressAndHold: (mouse) => {
+ if (mouse.source === Qt.MouseEventNotSynthesized)
+ contextMenu.popup()
}
Menu {
id: contextMenu
-
- MenuItem {
- text: qsTr("Do stuff")
- }
- MenuItem {
- text: qsTr("Do more stuff")
- }
+ MenuItem { text: "Cut" }
+ MenuItem { text: "Copy" }
+ MenuItem { text: "Paste" }
}
-//! [children]
}
+//! [root]
diff --git a/src/quickcontrols/doc/snippets/qtquickcontrols-menu-text-editing-contextmenu.qml b/src/quickcontrols/doc/snippets/qtquickcontrols-menu-text-editing-contextmenu.qml
deleted file mode 100644
index 6410998ffe..0000000000
--- a/src/quickcontrols/doc/snippets/qtquickcontrols-menu-text-editing-contextmenu.qml
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright (C) 2025 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-import QtQuick
-import QtQuick.Controls
-
-Item {
-//! [children]
- TextArea {
- text: qsTr("TextArea")
-
- // Disable the built-in context menu (since Qt 6.9).
- ContextMenu.menu: null
-
- TapHandler {
- acceptedButtons: Qt.RightButton
- onPressedChanged: {
- if (pressed === (Application.styleHints.contextMenuTrigger === Qt.ContextMenuTrigger.Press))
- contextMenu.popup()
- }
- }
- }
-
- Menu {
- id: contextMenu
-
- MenuItem {
- text: qsTr("Cut")
- // ...
- }
- MenuItem {
- text: qsTr("Copy")
- // ...
- }
- MenuItem {
- text: qsTr("Paste")
- // ...
- }
- }
-//! [children]
-}
diff --git a/src/quicktemplates/qquickmenu.cpp b/src/quicktemplates/qquickmenu.cpp
index 7b49fb7ef3..d002087efd 100644
--- a/src/quicktemplates/qquickmenu.cpp
+++ b/src/quicktemplates/qquickmenu.cpp
@@ -11,8 +11,6 @@
#endif
#include "qquickmenuseparator_p.h"
#include "qquicknativemenuitem_p.h"
-#include "qquickoverlay_p.h"
-#include "qquickoverlay_p_p.h"
#include "qquickpopupitem_p_p.h"
#include "qquickpopuppositioner_p_p.h"
#include "qquickaction_p.h"
@@ -73,7 +71,12 @@ static const int SUBMENU_DELAY = 225;
\li Popup menus; for example, a menu that is shown after clicking a button
\endlist
- For context menus, see \l {Context Menus}.
+ When used as a context menu, the recommended way of opening the menu is to call
+ \l popup(). Unless a position is explicitly specified, the menu is positioned at
+ the mouse cursor on desktop platforms that have a mouse cursor available, and
+ otherwise centered over its parent item.
+
+ \snippet qtquickcontrols-menu-contextmenu.qml root
When used as a popup menu, it is easiest to specify the position by specifying
the desired \l {Popup::}{x} and \l {Popup::}{y} coordinates using the respective
@@ -104,26 +107,6 @@ static const int SUBMENU_DELAY = 225;
Although \l {MenuItem}{MenuItems} are most commonly used with Menu, it can
contain any type of item.
- \section1 Context Menus
-
- For context menus, it is easier to use the \l ContextMenu attached type,
- which creates a menu upon a platform-specific event. In addition, text
- editing controls such as \l TextField and \l TextArea provide their own
- context menus by default.
-
- If not using \c ContextMenu, the recommended way of opening the menu is to
- call \l popup(). Unless a position is explicitly specified, the menu is
- positioned at the mouse cursor on desktop platforms that have a mouse
- cursor available, and otherwise centered over its parent item:
-
- \snippet qtquickcontrols-menu-contextmenu.qml children
-
- Note that if you are implementing your own context menu for text editing
- controls, you only need to show it on desktop platforms, as iOS and Android
- have their own native context menus:
-
- \snippet qtquickcontrols-menu-text-editing-contextmenu.qml children
-
\section1 Margins
As it is inherited from Popup, Menu supports \l {Popup::}{margins}. By
@@ -1685,23 +1668,8 @@ void QQuickMenu::setVisible(bool visible)
if (visible) {
// If a right mouse button event opens a menu, don't synthesize QContextMenuEvent
// (avoid opening redundant menus, e.g. in parent items).
- auto *window = this->window();
- Q_ASSERT(window);
- QQuickWindowPrivate::get(window)->rmbContextMenuEventEnabled = false;
- // Also, if users have their own custom non-ContextMenu-based text editing context menus,
- // we want those to take priority over our own. The check above handles that when
- // the user opens their menu on press, but not on release. For that, we close all
- // other menus that are open, assuming that we're not a sub-menu.
- if (!d->parentMenu) {
- QQuickOverlay *overlay = QQuickOverlay::overlay(window);
- if (overlay) {
- const QList<QQuickPopup *> allPopups = QQuickOverlayPrivate::get(overlay)->allPopups;
- for (auto *popup : allPopups) {
- if (popup != this && qobject_cast<QQuickMenu *>(popup))
- popup->close();
- }
- }
- }
+ Q_ASSERT(window());
+ QQuickWindowPrivate::get(window())->rmbContextMenuEventEnabled = false;
}
if (visible && ((d->useNativeMenu() && !d->maybeNativeHandle())
diff --git a/tests/auto/quickcontrols/qquicktextarea/data/customContextMenuOnRelease.qml b/tests/auto/quickcontrols/qquicktextarea/data/customContextMenuOnRelease.qml
deleted file mode 100644
index c3e66f1ea9..0000000000
--- a/tests/auto/quickcontrols/qquicktextarea/data/customContextMenuOnRelease.qml
+++ /dev/null
@@ -1,44 +0,0 @@
-import QtQuick
-import QtQuick.Controls
-
-Item {
- width: 400
- height: 400
-
- property alias textArea: textArea
- property alias userContextMenu: userContextMenu
- property alias ourContextMenu: ourContextMenu
-
- TextArea {
- id: textArea
- anchors.fill: parent
-
- ContextMenu.menu: Menu {
- id: ourContextMenu
- objectName: "ourContextMenu"
- popupType: Popup.Item
-
- MenuItem {
- text: "ContextMenu menu item"
- }
- }
-
- TapHandler {
- acceptedButtons: Qt.RightButton
- onTapped: userContextMenu.popup()
- }
- }
-
- Menu {
- id: userContextMenu
- objectName: "userContextMenu"
-
- MenuItem {
- text: qsTr("Font...")
- }
-
- MenuItem {
- text: qsTr("Color...")
- }
- }
-}
diff --git a/tests/auto/quickcontrols/qquicktextarea/tst_qquicktextarea.cpp b/tests/auto/quickcontrols/qquicktextarea/tst_qquicktextarea.cpp
index fbadaef315..1cf4a56e3b 100644
--- a/tests/auto/quickcontrols/qquicktextarea/tst_qquicktextarea.cpp
+++ b/tests/auto/quickcontrols/qquicktextarea/tst_qquicktextarea.cpp
@@ -20,10 +20,8 @@
#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
#include <QtQuickTemplates2/private/qquickpopupwindow_p_p.h>
#include <QtQuickTemplates2/private/qquicktextarea_p.h>
-#include <QtQuickControlsTestUtils/private/controlstestutils_p.h>
#include <QtQuickControlsTestUtils/private/qtest_quickcontrols_p.h>
-using namespace QQuickControlsTestUtils;
using namespace QQuickVisualTestUtils;
class tst_QQuickTextArea : public QQmlDataTest
@@ -43,8 +41,6 @@ private slots:
void contextMenuPaste();
void contextMenuDelete();
void contextMenuSelectAll();
- void customContextMenuOnRelease_data();
- void customContextMenuOnRelease();
private:
QScopedPointer<QPointingDevice> touchDevice = QScopedPointer<QPointingDevice>(QTest::createTouchDevice());
@@ -467,39 +463,6 @@ void tst_QQuickTextArea::contextMenuSelectAll()
QTRY_VERIFY(!contextMenu->menu()->isVisible());
}
-void tst_QQuickTextArea::customContextMenuOnRelease_data()
-{
- QTest::addColumn<QQuickPopup::PopupType>("popupType");
-
- QTest::newRow("Item") << QQuickPopup::Item;
- if (arePopupWindowsSupported())
- QTest::newRow("Window") << QQuickPopup::Window;
-}
-
-void tst_QQuickTextArea::customContextMenuOnRelease()
-{
- QFETCH(QQuickPopup::PopupType, popupType);
-
- QQuickView window;
- QVERIFY(QQuickTest::showView(window, testFileUrl("customContextMenuOnRelease.qml")));
- window.requestActivate();
- QVERIFY(QTest::qWaitForWindowActive(&window));
-
- auto *ourContextMenu = window.rootObject()->property("ourContextMenu").value<QQuickMenu *>();
- QVERIFY(ourContextMenu);
- ourContextMenu->setPopupType(popupType);
-
- // Right click on the TextArea to open the context menu. The user's custom context menu
- // should be visible, but not ours (ContextMenu's).
- auto *textArea = window.rootObject()->property("textArea").value<QQuickTextArea *>();
- QVERIFY(textArea);
- QTest::mouseClick(&window, Qt::RightButton, Qt::NoModifier, mapCenterToWindow(textArea));
- auto *userContextMenu = window.rootObject()->property("userContextMenu").value<QQuickMenu *>();
- QVERIFY(userContextMenu);
- QTRY_VERIFY(userContextMenu->isOpened());
- QTRY_VERIFY_WITH_TIMEOUT(!ourContextMenu->isVisible(), 1000);
-}
-
QTEST_QUICKCONTROLS_MAIN(tst_QQuickTextArea)
#include "tst_qquicktextarea.moc"