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
-
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