Unsolved Prevent Menu from closing when clicking a MenuItem in Quick Controls 2
-
Hi,
I've been searching this for a while now, and I couldn't find a solution yet.
I'm trying to create a nested menu, which has multiple MenuItems as children, using Quick Controls v2.
I'm able to do this without any issue.This menu is supposed to be a set of filters to be applied to my data.
All of this works perfectly, except for one User Experience issue: Whenever a MenuItem is clicked, the whole menu is dismissed. I want the menu to stay open - so the user can select multiple items without needing to navigate through the nested menu - until the user decides to click outside of it, to effectively close it.How is that possible?
I've tried to search for signal overriding for clicks on QML, but I couldn't find anything about it.
I also saw the C++ code for it, but I couldn't find where the menu is dismissed (I see it's probably onQQuickAbstractButtonPrivate::trigger()
, but I couldn't follow the signals).I'd appreciate if you guys could give me a snippet that can do that, or instructions on how to tackle this problem.
Thanks in advance!
-
I'd say use a
Popup
instead of aMenu
-
Using a popup would force me to recreate the entire logic of Menu, including nested menus, right?
Is there a simpler way to do that?
If not, can you provide an example of how to implement nested menus using a popup, as a base?
-
Is there any update to this topic? Seeking solution to the same problem, but like OP recreating entire menu logic seems like to much effort
-
Had the same Problem.
My workaround was to have 2 identical menus. If an item is clicked it opens the the other menu. As the 2 menus are equal it appears as one menu to stay open. -
Similar question already here on the forum with a usable answer;
https://forum.qt.io/topic/70510/not-to-close-menu-on-menuitem-click
-
The proposed workaround in the old forum topic (use CheckBoxes) isn't very good because that stops the highlight logic from working.
The root cause of this issue is that
Menu
connects thetriggered()
signal of a childMenuItem
to its own internal signal handler that closes the menu unconditionally if the item doesn't have a submenu.triggered()
is connected to the underlyingAbstractButton
'sclicked()
signal byMenuItem
itself. This leaves us with two work arounds that I can think of:- Manually disconnect
clicked()
fromtriggered()
in C++. - Prevent the
clicked()
signal from being raised in the first place, by blocking mouse events from reaching theMenuItem
. I've implemented this workaround below:
Menu { id: menu ... MenuItem { id: menuItem checkable: true checked: ... text: ... down: mouseArea.pressed MouseArea { id: mouseArea anchors.fill: parent hoverEnabled: false acceptedButtons: Qt.LeftButton onClicked: function(mouse) { menuItem.toggle() menuItem.menuItemActivated() if(mouse.modifiers & Qt.ControlModifier) { return } menu.dismiss() } } onTriggered: menuItemActivated() function menuItemActivated() { ... } } }
In this example, the menu doesn't go away if they're holding control when clicking.
- Manually disconnect