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. Menubar in separate file
Forum Updated to NodeBB v4.3 + New Features

Menubar in separate file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 906 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.
  • G Offline
    G Offline
    gueny
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by jpnurmi
      #2

      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. :)

      1 Reply Last reply
      2
      • G Offline
        G Offline
        gueny
        wrote on last edited by
        #3

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

        Thanks! :)

        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