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. How to assign a function definition to a property in QML1 (QtQuick 1.1)?

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 2.3k Views
  • 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.
  • I Offline
    I Offline
    imran20487
    wrote on last edited by
    #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(){
                          ...
                       }
      }
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • I imran20487

      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(){
                            ...
                         }
        }
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        imran20487
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Maheshdev305
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imran20487
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

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