Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved How do I dynamically add a new menu item to a menu?

    QML and Qt Quick
    4
    6
    299
    Loading More Posts
    • 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.
    • M
      mirro last edited by mirro

      I use the following example to make an error in QT5.9.7.
      What went wrong in the following example?

      import QtQuick 2.9
      import QtQuick.Controls 2.3
      import QtQuick.Window 2.2
      
      ApplicationWindow {
          visible: true
          width: 640
          height: 200
          title: qsTr("Hello World")
          id:root
          
          MouseArea {
          
              anchors.fill: parent
          
              onClicked: {
          
                  menu.addEntry('test')
          
                  menu.open()
          
              }
          
          }
          
          
          
          Menu {
          
              id: menu
          
              function addEntry(title) {
          
                  menu.addItem(menuItem.createObject(menu, { text: title }))
          
              }
          
          
          
              Component {
          
                  id: menuItem
          
                  MenuItem {
                         text: title 
                  }
          
              }
          
          }
      jsulm 1 Reply Last reply Reply Quote 0
      • fcarney
        fcarney last edited by

        @mirro
        Instantiators are much cleaner.

        https://stackoverflow.com/questions/45652525/qml-filling-menu-with-model-items

        @Markkyboy is telling you to search and research your problem more. We have been getting a LOT of low effort posts and it wears on your patience.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply Reply Quote 2
        • Markkyboy
          Markkyboy last edited by

          RTFM!

          https://doc.qt.io/qt-5/qml-qtquick-controls2-menu.html

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

          I live by the sea, not in it.

          M 1 Reply Last reply Reply Quote 1
          • M
            mirro @Markkyboy last edited by

            @Markkyboy What do you mean?I am using 5.9

            Markkyboy 1 Reply Last reply Reply Quote 0
            • fcarney
              fcarney last edited by

              @mirro
              Instantiators are much cleaner.

              https://stackoverflow.com/questions/45652525/qml-filling-menu-with-model-items

              @Markkyboy is telling you to search and research your problem more. We have been getting a LOT of low effort posts and it wears on your patience.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply Reply Quote 2
              • jsulm
                jsulm Lifetime Qt Champion @mirro last edited by

                @mirro said in How do I dynamically add a new menu item to a menu?:

                What went wrong in the following example?

                Is this some kind of brain twister?
                Don't you think you should tell others what is not working instead to ask others to find out by themself?
                Please ask understandable questions...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • Markkyboy
                  Markkyboy @mirro last edited by

                  @mirro - seems I misunderstood the question. I don't recall seeing the word 'dynamically' in the question title, please ignore my initial response.

                  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 Reply Quote 0
                  • First post
                    Last post