How to assign a function definition to a property in QML1 (QtQuick 1.1)?



  • MyItem.qml

    import QtQuick 1.1
    Rectangle{      
      property variant funcDelegate
      function processRequest(){
         funcDelegate()//This call throws error with approach 1
      }
    }
    

    I have tried these below 2 approaches (not working)

    Approach 1:

    TypeError: Result of expression 'funcDelegate' [undefined] is not a function.

    Main.qml

    import QtQuick 1.1
    Item{
      width: 600
      height: 600
      focus: true
      
      MyItem{
    
        Component.onCompleted: {
           funcDelegate = process
        }
    
        function process() {
           ...
        }
      }
    }
    

    Approach 2:

    I get the error when compiling

    Error: Unable to assign a function to a property.

    Main.qml

    import QtQuick 1.1
    Item{
      width: 600
      height: 600
      focus: true
      MyItem{
        //assign the property with a function (This works in Qt5 not in 
          Qt 4)
        funcDelegate : function process(){
                          ...
                       }
      }
    }


  • @imran20487 you call functions with the actual parenthesize attached.

    try

    Component.onCompleted: {
           funcDelegate = process()
        }
    

    process also needs to have an actuall return value

    function process() {
           ...
           return something
        }
    


  • I can't call it with the actual paranthesis, because it would call that function immediately. What I want is that it should be called by MyItem.qml in ProcessRequest() as a callback



  • @imran20487 the below code works well. try it yourself.

    Item {
     id: rootItem
     property var funcPtr
    
        function test1() {
            console.log("Test 1")
        }
        function test2() {
            console.log("Test 2")
        }
     function processSomething() {
            funcPtr()
        }
    
        Row {
            Rectangle {
                height: 50
                width: 50
                color:"Red"
    
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        rootItem.funcPtr = test1
                        rootItem.funcPtr()
                    }
                }
            }
            Rectangle {
                height: 50
                width: 50
                color:"blue"
    
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        rootItem.funcPtr = test2
                        rootItem.funcPtr()
                    }
                }
            }
        }
    }
    
    

    Either you can call the var funcPtr directly, or alternatively you can use it in another function like processSomething().
    Before using funcPtr, check if it is undefined, or else, it might behave unexpectedly.



  • @Maheshdev305 Thanks for the reply, your solution works for QML2 (QT5, QtQuick 2) since 'var' type is avialable, my problem is with QML1 (QT4, QtQuick 1) where 'var' type is not available.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.