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. How can I use MenuBar.insertMenu method in QML?
QtWS25 Last Chance

How can I use MenuBar.insertMenu method in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlmenubarmenuinsertmenu
5 Posts 2 Posters 907 Views
  • 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.
  • X Offline
    X Offline
    X Grant
    wrote on 9 Jul 2020, 14:37 last edited by
    #1

    I am trying to create a dynamic menu in QML, the code is as follows:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    
    ApplicationWindow {
        id: root
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        menuBar: MenuBar {
            Menu {
                title: "FIRST"
            }
    
            Menu {
                title: "SECOND"
            }
    
            Menu {
                title: "THIRD"
            }
        }
    
        Component {
            id: myMenu
    
            Menu {
                title: "ZERO"
            }
        }
    
        Component.onCompleted: {
            var menu1 = myMenu.createObject(menuBar);
            menuBar.insertMenu(0, menu1);
        }
    }
    

    The system prompts the following error message:

    QQuickItem::stackBefore: Cannot stack MenuBarItem_QMLTYPE_7(0x28607824d40, parent=0x28606ee4660, geometry=122,0 58x40) before MenuBarItem_QMLTYPE_7(0x28607824d40), which must be a sibling

    And the order of the menu is: SECOND, THIRD, FIRST, ZERO. I really don't understand. Why?

    How can I set the correct order of menus: ZERO, FIRST, SECOND, THIRD?

    Thank you!

    O 1 Reply Last reply 9 Jul 2020, 17:29
    0
    • X X Grant
      9 Jul 2020, 14:37

      I am trying to create a dynamic menu in QML, the code is as follows:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 2.12
      
      ApplicationWindow {
          id: root
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          menuBar: MenuBar {
              Menu {
                  title: "FIRST"
              }
      
              Menu {
                  title: "SECOND"
              }
      
              Menu {
                  title: "THIRD"
              }
          }
      
          Component {
              id: myMenu
      
              Menu {
                  title: "ZERO"
              }
          }
      
          Component.onCompleted: {
              var menu1 = myMenu.createObject(menuBar);
              menuBar.insertMenu(0, menu1);
          }
      }
      

      The system prompts the following error message:

      QQuickItem::stackBefore: Cannot stack MenuBarItem_QMLTYPE_7(0x28607824d40, parent=0x28606ee4660, geometry=122,0 58x40) before MenuBarItem_QMLTYPE_7(0x28607824d40), which must be a sibling

      And the order of the menu is: SECOND, THIRD, FIRST, ZERO. I really don't understand. Why?

      How can I set the correct order of menus: ZERO, FIRST, SECOND, THIRD?

      Thank you!

      O Offline
      O Offline
      ODБOï
      wrote on 9 Jul 2020, 17:29 last edited by ODБOï 7 Sept 2020, 19:32
      #2

      @X-Grant hi,
      I'm not sure where the problem comes from, looks like you can insert a Menu at the end but not at index 0..
      but that does not seem to be documented

      a workaround : insert at the end then rearrange the menus

      
          Component.onCompleted:{
              var menu1 = myMenu.createObject(menuBar);
              
              menuBar.insertMenu(root.menuBar.menus.length, menu1);
      
              var menus = root.menuBar.menus
              var menusLength = menus.length
      
              for (var i = 0; i < menusLength-1; i++){
                  menuBar.moveItem(0,menusLength-i+1)
              }
          }
      
      1 Reply Last reply
      1
      • X Offline
        X Offline
        X Grant
        wrote on 11 Jul 2020, 01:27 last edited by
        #3

        Thank you for your reply.
        The code can add the required menu indeed.
        But it can't execute many times.

        import QtQuick 2.12
        import QtQuick.Window 2.12
        import QtQuick.Controls 2.12
        
        ApplicationWindow {
            id: root
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
        
            menuBar: MenuBar {
                Menu {
                    title: "FIRST"
                }
        
                Menu {
                    title: "SECOND"
                }
        
                Menu {
                    title: "THIRD"
                }
            }
        
            Component {
                id: myMenu
        
                Menu {
                    title: "ZERO"
        
                    Action {
                        text: "ACTION_0"
                    }
                }
            }
        
            Button {
                text: "ADD"
                onClicked: {
                    root.add_menu(myMenu);
                }
            }
        
            function add_menu(menu_comp_id) {
                var menu1 = menu_comp_id.createObject(menuBar);
        
                menuBar.insertMenu(root.menuBar.menus.length, menu1);
        
                var menus = root.menuBar.menus;
                var menusLength = menus.length;
        
                for (var i = 0; i < menusLength-1; i++){
                    menuBar.moveItem(0, menusLength-i+1)
                }
            }
        }
        

        When I add menu 4 times, the result is not correctly.
        Is it a Qt bug? The insertMenu function is inconsistent with the document.

        O 1 Reply Last reply 11 Jul 2020, 11:24
        0
        • X X Grant
          11 Jul 2020, 01:27

          Thank you for your reply.
          The code can add the required menu indeed.
          But it can't execute many times.

          import QtQuick 2.12
          import QtQuick.Window 2.12
          import QtQuick.Controls 2.12
          
          ApplicationWindow {
              id: root
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              menuBar: MenuBar {
                  Menu {
                      title: "FIRST"
                  }
          
                  Menu {
                      title: "SECOND"
                  }
          
                  Menu {
                      title: "THIRD"
                  }
              }
          
              Component {
                  id: myMenu
          
                  Menu {
                      title: "ZERO"
          
                      Action {
                          text: "ACTION_0"
                      }
                  }
              }
          
              Button {
                  text: "ADD"
                  onClicked: {
                      root.add_menu(myMenu);
                  }
              }
          
              function add_menu(menu_comp_id) {
                  var menu1 = menu_comp_id.createObject(menuBar);
          
                  menuBar.insertMenu(root.menuBar.menus.length, menu1);
          
                  var menus = root.menuBar.menus;
                  var menusLength = menus.length;
          
                  for (var i = 0; i < menusLength-1; i++){
                      menuBar.moveItem(0, menusLength-i+1)
                  }
              }
          }
          

          When I add menu 4 times, the result is not correctly.
          Is it a Qt bug? The insertMenu function is inconsistent with the document.

          O Offline
          O Offline
          ODБOï
          wrote on 11 Jul 2020, 11:24 last edited by ODБOï 7 Nov 2020, 11:25
          #4

          @X-Grant said in How can I use MenuBar.insertMenu method in QML?:

          But it can't execute many times.

          if you read the code carefully you will see that's normal.. menuBar.moveItem(0, menusLength-i+1)

          Anyway, yes, to me it look like a bug with insertMenu()..

          What are you trying to achive exactly ?
          How many menus are you planning to add ? is it an option to create menus statically and hide/show them when its needed ?

          X 1 Reply Last reply 13 Jul 2020, 07:45
          0
          • O ODБOï
            11 Jul 2020, 11:24

            @X-Grant said in How can I use MenuBar.insertMenu method in QML?:

            But it can't execute many times.

            if you read the code carefully you will see that's normal.. menuBar.moveItem(0, menusLength-i+1)

            Anyway, yes, to me it look like a bug with insertMenu()..

            What are you trying to achive exactly ?
            How many menus are you planning to add ? is it an option to create menus statically and hide/show them when its needed ?

            X Offline
            X Offline
            X Grant
            wrote on 13 Jul 2020, 07:45 last edited by
            #5

            @LeLev Yes. I have created some menus statically, hide/show them manually. Thank you.

            1 Reply Last reply
            0

            5/5

            13 Jul 2020, 07:45

            • Login

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