Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I use MenuBar.insertMenu method in QML?



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



  • @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)
            }
        }
    


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



  • @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 ?



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