Qt Forum

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

    Discover and share your #QtStories


    Fill out our CSRD Survey


    Solved How to add MenuItem dynamically (text is not set)

    QML and Qt Quick
    3
    7
    314
    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.
    • P
      PawlosCK last edited by 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 1 Reply Last reply Reply Quote 0
      • P
        PawlosCK last edited by PawlosCK

        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 Reply Quote 0
        • B
          Bob64 @PawlosCK last edited by

          @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 Reply Quote 0
          • P
            PawlosCK last edited by

            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 Reply Quote 0
            • P
              PawlosCK last edited by

              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 Reply Quote 0
              • P
                PawlosCK last edited by PawlosCK

                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 Reply Quote 0
                • fcarney
                  fcarney last edited by

                  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 Reply Quote 0
                  • P
                    PawlosCK last edited by PawlosCK

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