Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Creating Menu Action on the fly?



  • I am doing this:

        property var resolutions: [[1920,1080],[1280,800],[1024,600]]
    
        Component.onCompleted: {
            for(var ind in resolutions){
                var res = resolutions[ind]
                console.log("(%1:%2,%3)".arg(res[0]).arg(res[1]).arg(res[0]/res[1]))
            }
    
            // create the component
            var component = Qt.createComponent("ResAction.qml");
    
            // add actions to resmenu
            for(var index in resolutions){
                var res = resolutions[index]
    
                // create instance
                var action = component.createObject(resmenu,
                    {"text":"(%1:%2,%3)".arg(res[0]).arg(res[1]).arg(res[0]/res[1]),
                     "obj":mainwin,
                     "res":res});
    
                // add instance to menu
                resmenu.addItem(action)
            }
        }
    
    ...
    
    Menu {
                id: resmenu
                anchors.centerIn: parent
    }
    

    ResAction.qml:

    import QtQuick.Controls 2.5
    
    Action {
        text: "undefined"
        property var obj: undefined
        property var res: [-1,-1]
    
        onTriggered: {
            obj.width = res[0]
            obj.height = res[1]
        }
    }
    

    But it gives this error on the resmenu.addItem(action) line:

    "Could not convert argument 0 at"
    	 "onCompleted@qrc:/main2.qml:50"
    

    Can Actions be created on the fly like this?



  • @fcarney said in Creating Menu Action on the fly?:

    Can Actions be created on the fly like this?

    Ummm... yes they can be if I actually add an action:

    resmenu.addAction(action)
    

Log in to reply