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 to add MenuItem dynamically (text is not set)

How to add MenuItem dynamically (text is not set)

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.1k Views 2 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.
  • P Offline
    P Offline
    PawlosCK
    wrote on last edited by PawlosCK
    #1

    I am trying to add menuItem dynamically and it works, except text field.
    I added this into Menu object

                Component.onCompleted:
                {
                    var xx = "aaaaaaa"
                    var item = Qt.createQmlObject("import QtQuick 2.13; import QtQuick.Controls 2.13; MenuItem {}", submenuAlarm)
                    item.text = "WWWWWWW"
                    submenuAlarm.addItem(item)
                }
    

    As text is set something like this "MenuItem_QMLTYPE_98...."

    B 1 Reply Last reply
    0
    • P Offline
      P Offline
      PawlosCK
      wrote on last edited by PawlosCK
      #5

      I found solution. It works perfectly. This code is inside Menu object.

                  Component.onCompleted:
                  {
                      var item = submenuAlarm.addItem("New Item")
                      item.triggered.connect(function(){console.log("Value: " + item.text)})
                  }
      
      1 Reply Last reply
      0
      • P PawlosCK

        I am trying to add menuItem dynamically and it works, except text field.
        I added this into Menu object

                    Component.onCompleted:
                    {
                        var xx = "aaaaaaa"
                        var item = Qt.createQmlObject("import QtQuick 2.13; import QtQuick.Controls 2.13; MenuItem {}", submenuAlarm)
                        item.text = "WWWWWWW"
                        submenuAlarm.addItem(item)
                    }
        

        As text is set something like this "MenuItem_QMLTYPE_98...."

        B Offline
        B Offline
        Bob64
        wrote on last edited by
        #2

        @PawlosCK on my phone so can't do a detailed reply, but see this StackOverflow item - I think it will help: https://stackoverflow.com/a/44047556

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PawlosCK
          wrote on last edited by
          #3

          The same. Text is not set correctly.

                  Menu
                  {
                      id: submenuAlarm
                      title: "Alarms"
          
                      function addEntry(title) {
                          submenuAlarm.addItem(menuItem.createObject(menu, { text: title }))
                      }
          
                      Component
                      {
                          id: menuItem
                          MenuItem {}
                      }
          
                      Component.onCompleted:
                      {
                          submenuAlarm.addEntry('test')
                          submenuAlarm.addEntry('test2')
                      }
          }
          
          1 Reply Last reply
          0
          • P Offline
            P Offline
            PawlosCK
            wrote on last edited by
            #4

            I found other solution, but in this case, I don't know how to get text or other variable from Menuitem

            This code is inside Menu object.

                        Action
                        {
                            id: getValueAlarm
                            onTriggered: console.log("ssflkdfslkdfjlkfjlkfjkf: " + title)
                        }
            
                        Component.onCompleted:
                        {
                            var item = addItem("New Item")
                            item.action = getValueAlarm
                        }
            
            1 Reply Last reply
            0
            • P Offline
              P Offline
              PawlosCK
              wrote on last edited by PawlosCK
              #5

              I found solution. It works perfectly. This code is inside Menu object.

                          Component.onCompleted:
                          {
                              var item = submenuAlarm.addItem("New Item")
                              item.triggered.connect(function(){console.log("Value: " + item.text)})
                          }
              
              1 Reply Last reply
              0
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #6

                I create a model if I need more than one MenuItem:

                import QtQuick 2.12 //2.3
                import QtQuick.Controls 2.12 //1.2
                import QtQuick.Dialogs 1.2 //1.0
                import QtQml 2.12
                
                ApplicationWindow {
                    id: main_window
                
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Hello World")
                
                    menuBar : MenuBar {
                
                        Menu {
                            id: recentFilesMenu
                
                            title: "File"
                
                            MenuItem {
                                text: "Open"
                                onTriggered: fileDialog.visible = true
                            }
                
                            MenuSeparator { }
                
                            Menu {
                                id: prevfiles
                
                                title: "Prev Files"
                
                
                            }
                
                            TextMetrics {
                                id: file_action_metrics
                                text: {
                                    //console.log(instantiator.model.get(0))
                                    for(var i in instantiator.model.get(0)){
                                        console.log(i, instantiator.model.get(0)[i])
                                    }
                
                                    var txt = instantiator.model.count > 0 ? instantiator.model.get(0).fileName : ""
                                    return txt
                                }
                            }
                
                            Instantiator {
                                id: instantiator
                                model: ListModel { id: files }
                
                                MenuItem {
                                    //width: prevfiles.width
                
                                    Text {
                                        anchors.verticalCenter: parent.verticalCenter
                                        text: file_action.text
                                    }
                
                                    MouseArea {
                                        id: file_action_ma
                
                                        anchors.fill: parent
                                        hoverEnabled: true
                                    }
                
                                    ToolTip.delay: 500
                                    ToolTip.timeout: 5000
                                    ToolTip.text: file_action.text
                                    ToolTip.visible: file_action_ma.containsMouse
                
                                    Action {
                                        id: file_action
                                        text: fileName
                                    }
                                }
                
                                //onObjectAdded: prevfiles.insertAction(index, object)
                                //onObjectRemoved: prevfiles.removeAction(object)
                                onObjectAdded: prevfiles.insertItem(index, object)
                                onObjectRemoved: prevfiles.removeItem(object)
                            }
                
                            MenuSeparator { visible: files.count > 0 }
                
                            MenuItem { text: "Exit" }
                        }
                    }
                
                    FileDialog {
                        id: fileDialog
                        title: "Open"
                        onAccepted: {
                            for(var i = 0; i < fileDialog.fileUrls.length; ++i)
                                files.append({fileName: fileDialog.fileUrls[i]})
                        }
                    }
                }
                

                This code uses an Instantiator to manage new MenuItems.

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  PawlosCK
                  wrote on last edited by PawlosCK
                  #7

                  Below full code of Menu with adding and reading menuitem

                          Menu
                                  {
                                      id: submenuAlarms
                                      title: "Alarms"
                          
                                      function createMenuItemDefault()
                                      {
                                          var defaultAlarms = ["30s", "1m", "3m", "5m", "10m", "20m", "30m", "1h"];
                                          var numberOfAlarms = defaultAlarms.length
                                          var alarmsItems = [numberOfAlarms];
                                          var index = 0
                                          for (var alarm of defaultAlarms)
                                          {
                                              console.log(alarm)
                                              var item = submenuAlarms.addItem(alarm)
                                              item.id = "ID_"+alarm
                                              item.triggered.connect(function(){console.log("Value: " + alarmsItems[__selectedIndex])})
                                              alarmsItems[index] = item.text
                                              index++
                                          }
                                          return alarmsItems
                                      }
                          
                                      Component.onCompleted:
                                      {
                                          createMenuItemDefault()
                                      }
                              }
                  
                  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