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. Creating Menu Action on the fly?

Creating Menu Action on the fly?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 408 Views 1 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    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?

    C++ is a perfectly valid school of magic.

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

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

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      3

      • Login

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