Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Prevent Menu from closing when clicking a MenuItem in Quick Controls 2
Forum Updated to NodeBB v4.3 + New Features

Prevent Menu from closing when clicking a MenuItem in Quick Controls 2

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 6 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheZoc
    wrote on last edited by
    #1

    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 on QQuickAbstractButtonPrivate::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!

    1 Reply Last reply
    2
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      I'd say use a Popup instead of a Menu

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TheZoc
        wrote on last edited by
        #3

        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?

        ocgltdO 1 Reply Last reply
        0
        • T TheZoc

          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?

          ocgltdO Offline
          ocgltdO Offline
          ocgltd
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            josef4qt
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • MarkkyboyM Offline
              MarkkyboyM Offline
              Markkyboy
              wrote on last edited by
              #6

              Similar question already here on the forum with a usable answer;

              https://forum.qt.io/topic/70510/not-to-close-menu-on-menuitem-click

              Don't just sit there standing around, pick up a shovel and sweep up!

              I live by the sea, not in it.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Lord_Naikon
                wrote on last edited by
                #7

                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 the triggered() signal of a child MenuItem to its own internal signal handler that closes the menu unconditionally if the item doesn't have a submenu.

                triggered() is connected to the underlying AbstractButton's clicked() signal by MenuItem itself. This leaves us with two work arounds that I can think of:

                1. Manually disconnect clicked() from triggered() in C++.
                2. Prevent the clicked() signal from being raised in the first place, by blocking mouse events from reaching the MenuItem. 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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved