Menubar in separate file



  • Total QT/QML newbie here, so sorry if I'm missing something obvious.
    I have a basic application window with a menu bar:

    //main.qml
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("My App")
    
        menuBar: MenuBar {
                          Menu {
                                title: qsTr("Game")
                                MenuItem { ... }
                                MenuItem { ... }
                          }
                  }
    
        //other stuff here
    }
    

    Now the MenuBar implementation is getting too long so I would like to put it in a separate file, say Menu.qml

    //Menu.qml
    MenuBar { id: bar
             Menu {
                   title: qsTr("Game")
                    MenuItem { ... }
                    MenuItem { ... }
             }
    }
    

    And then do (in main.qml) something like

    ApplicationWindow {
        //other code
       Menu { id: myMenu }
       menuBar: myMenu
    }
    

    But this fails with "Unable to assign Menu_QMLTYPE_28 to MenuBar_QMLTYPE_30". Coming from C++ this looks to me like a wrong "return type" so I thought I'd have to do something like

    //Menu.qml
    someOtherType{ id: otherid
        MenuBar { id: bar
             Menu {
                   title: qsTr("Game")
                    MenuItem { ... }
                    MenuItem { ... }
             }
        }
    }
    

    I tried some types like QObject and Item, but no luck. I also checked the documentation of MenuBar but didn't find anything helpful. How do I make this work?



  • There's a conflict between your own Menu QML type (Menu.qml) and the Menu QML type provided by Qt. While it is possible to resolve the conflict by using qualified/namespaced imports, I'd suggest inventing a better name for Menu.qml just for the sake of clarity. :)



  • Omg, it WAS as easy as renaming Menu.qml to My_Menu.qml and then calling My_Menu {} in main.qml...

    Thanks! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.