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 do I dynamically add a new menu item to a menu?

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 1.2k 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by mirro
    #1

    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 
                }
        
            }
        
        }
    jsulmJ 1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #4

      @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
      2
      • MarkkyboyM Offline
        MarkkyboyM Offline
        Markkyboy
        wrote on last edited by
        #2

        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
        1
        • MarkkyboyM Markkyboy

          RTFM!

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

          M Offline
          M Offline
          mirro
          wrote on last edited by
          #3

          @Markkyboy What do you mean?I am using 5.9

          MarkkyboyM 1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #4

            @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
            2
            • M 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 
                          }
                  
                      }
                  
                  }
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @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
              0
              • M mirro

                @Markkyboy What do you mean?I am using 5.9

                MarkkyboyM Offline
                MarkkyboyM Offline
                Markkyboy
                wrote on last edited by
                #6

                @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
                0

                • Login

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