Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

How to add MenuItem dynamically (text is not set)



  • 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...."



  • 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)})
                }
    


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



  • 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')
                }
    }
    


  • 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
                }
    


  • 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)})
                }
    


  • 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.



  • 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()
                        }
                }
    

Log in to reply