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 do QML objects pass in function parameters?
Forum Update on Tuesday, May 27th 2025

How do QML objects pass in function parameters?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 13.2k Views 2 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.
  • M Offline
    M Offline
    mirro
    wrote on 22 Aug 2020, 02:24 last edited by
    #1
    Item{
        id:root
        property string btnTest:""
       void setPos(Button curBtn)
       {
            btnTest = curBtn.text
       }
        Button{
            id:btn
            text:"TEST"
        }
    }
    
    A L 3 Replies Last reply 22 Aug 2020, 11:55
    0
    • L lawrence.emke
      23 Aug 2020, 03:57

      @Allon You should not pass QML objects as arguments in a function call.

      You have to understand what is happening behind the curtain.
      All function calls are a "pass by value". This means that a "copy"
      of your object is passed to your function (objects are not small things and have a lot of pointers). The copied object is not the real object.
      Any changes you make will be to the copied object.

      If you want to pass an object to a function you need to pass a
      "pseudo value" which can be used as an identifier to locate the real object.

      So here is the process:
      Locate the object you want to pass.

      The "id" values do not exist at run time. You can not use it.

      So you need a property that DOES exist at run time. Qt has defined the "objectName" attribute to all "normal" objects. Its default value is the empty string. You can set it to any value you want. You can use this value to allow the called function to locate your object. You can pass it to any function because it is only a string. You can even change it at execution time.

      Have the called function use this value to locate the real object.

      Be aware: The objectName is a "string" there is no guarantee that it is unique. If you create your own object (a separate qml file). In that file you set the objectName value (and don't change it) then all instances of the object will have the same objectName value. You have to come up with the method to identify multiple occurrences. Or you should make it clear that this is an assumption that your application is dependent on.

      Hope this helps

      G Offline
      G Offline
      GrecKo
      Qt Champions 2018
      wrote on 23 Aug 2020, 13:57 last edited by GrecKo
      #7

      @lawrence-emke said in How do QML objects pass in function parameters?:

      @Allon You should not pass QML objects as arguments in a function call.
      All function calls are a "pass by value". This means that a "copy"
      of your object is passed to your function (objects are not small things and have a lot of pointers). The copied object is not the real object.
      Any changes you make will be to the copied object.

      That is incorrect, you can pass QML objects as arguments to a function, and they aren't passed as value if they are QObjects, but as pointers. Plain JS objects do get passed as value though.

      If you want to pass an object to a function you need to pass a
      "pseudo value" which can be used as an identifier to locate the real object.
      So here is the process:
      Locate the object you want to pass.
      The "id" values do not exist at run time. You can not use it.

      You can use id at runtime, it's not a property but it can be used to identify an object.

      Item {
          id: foo
      }
      Item {
          id: bar
          function func(item) { ... }
      }
      
      // calling bar.func(foo) works and bar can modify foo in func
      

      If you can do something else, don't rely on objectName to identify an object.

      As for @Allon 's actual problem, it is just a syntactic error.

      Instead of

                 function setRect(Object obj) { 
                       anchors.fill: obj
                 }  
      

      You should have

                 function setRect(obj) { 
                       anchors.fill = obj;
                 }  
      

      That code is correct but doesn't like good practice QML, too imperative. I would advice you to rethink what you are doing.

      A 1 Reply Last reply 23 Aug 2020, 17:06
      0
      • M mirro
        22 Aug 2020, 02:24
        Item{
            id:root
            property string btnTest:""
           void setPos(Button curBtn)
           {
                btnTest = curBtn.text
           }
            Button{
                id:btn
                text:"TEST"
            }
        }
        
        A Offline
        A Offline
        Allon
        wrote on 22 Aug 2020, 11:55 last edited by
        #2

        Hi,
        You usually do not as you can access elements properties directly. Here is an example where I get the button text directly from the button text property without using paramaters:

        import QtQuick 2.12
        import QtQuick.Window 2.12
        import QtQuick.Controls 2.12
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
        
        
            function showButtonText() {
                console.log("My button text: " + btn.text )
            }
        
            Button{
                id:btn
        
                text: "TEST"
        
                MouseArea {
                    id: buttonMousearea
        
                    anchors.fill: parent
        
                    onClicked: {
                        showButtonText()
                    }
                }
            }
        }
        
        M 1 Reply Last reply 22 Aug 2020, 15:08
        0
        • A Allon
          22 Aug 2020, 11:55

          Hi,
          You usually do not as you can access elements properties directly. Here is an example where I get the button text directly from the button text property without using paramaters:

          import QtQuick 2.12
          import QtQuick.Window 2.12
          import QtQuick.Controls 2.12
          
          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
          
              function showButtonText() {
                  console.log("My button text: " + btn.text )
              }
          
              Button{
                  id:btn
          
                  text: "TEST"
          
                  MouseArea {
                      id: buttonMousearea
          
                      anchors.fill: parent
          
                      onClicked: {
                          showButtonText()
                      }
                  }
              }
          }
          
          M Offline
          M Offline
          mirro
          wrote on 22 Aug 2020, 15:08 last edited by
          #3

          @Allon Hello, if I have to pass in QML object to a function argument, what do I do?

          A 1 Reply Last reply 22 Aug 2020, 18:17
          0
          • M mirro
            22 Aug 2020, 15:08

            @Allon Hello, if I have to pass in QML object to a function argument, what do I do?

            A Offline
            A Offline
            Allon
            wrote on 22 Aug 2020, 18:17 last edited by
            #4

            @mirro What do you mean by passing in QML object to a function. Can you tell me what it could be used to?
            I did not understood your example.

            M 1 Reply Last reply 23 Aug 2020, 01:19
            0
            • A Allon
              22 Aug 2020, 18:17

              @mirro What do you mean by passing in QML object to a function. Can you tell me what it could be used to?
              I did not understood your example.

              M Offline
              M Offline
              mirro
              wrote on 23 Aug 2020, 01:19 last edited by
              #5

              @Allon I want to implement the following code,but this setRect(Object obj) syntax error.

              import QtQuick 2.12
              import QtQuick.Window 2.12
              import QtQuick.Controls 2.12
              
              Window {
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")
              
                 Rectangle{
                         id:rct
                         color: "red"
                         function setRect(Object obj) { 
                               anchors.fill: obj
                         }  
                 }
              
              
                  Button{
                      id:btn
              
                      text: "TEST"
              
                      MouseArea {
                          id: buttonMousearea
              
                          anchors.fill: parent
              
                          onClicked: {
                               rct.setRect(btn)
                          }
                      }
                  }
              
                    Button{
                      id:btn2
              
                      text: "TEST2"
              
                      MouseArea {
                          id: buttonMousearea
              
                          anchors.fill: parent
              
                          onClicked: {
                                  rct.setRect(btn2)
                          }
                      }
                  }
              }
              
              1 Reply Last reply
              0
              • M mirro
                22 Aug 2020, 02:24
                Item{
                    id:root
                    property string btnTest:""
                   void setPos(Button curBtn)
                   {
                        btnTest = curBtn.text
                   }
                    Button{
                        id:btn
                        text:"TEST"
                    }
                }
                
                L Offline
                L Offline
                lawrence.emke
                wrote on 23 Aug 2020, 03:57 last edited by lawrence.emke
                #6

                @Allon You should not pass QML objects as arguments in a function call.

                You have to understand what is happening behind the curtain.
                All function calls are a "pass by value". This means that a "copy"
                of your object is passed to your function (objects are not small things and have a lot of pointers). The copied object is not the real object.
                Any changes you make will be to the copied object.

                If you want to pass an object to a function you need to pass a
                "pseudo value" which can be used as an identifier to locate the real object.

                So here is the process:
                Locate the object you want to pass.

                The "id" values do not exist at run time. You can not use it.

                So you need a property that DOES exist at run time. Qt has defined the "objectName" attribute to all "normal" objects. Its default value is the empty string. You can set it to any value you want. You can use this value to allow the called function to locate your object. You can pass it to any function because it is only a string. You can even change it at execution time.

                Have the called function use this value to locate the real object.

                Be aware: The objectName is a "string" there is no guarantee that it is unique. If you create your own object (a separate qml file). In that file you set the objectName value (and don't change it) then all instances of the object will have the same objectName value. You have to come up with the method to identify multiple occurrences. Or you should make it clear that this is an assumption that your application is dependent on.

                Hope this helps

                G 1 Reply Last reply 23 Aug 2020, 13:57
                -1
                • L lawrence.emke
                  23 Aug 2020, 03:57

                  @Allon You should not pass QML objects as arguments in a function call.

                  You have to understand what is happening behind the curtain.
                  All function calls are a "pass by value". This means that a "copy"
                  of your object is passed to your function (objects are not small things and have a lot of pointers). The copied object is not the real object.
                  Any changes you make will be to the copied object.

                  If you want to pass an object to a function you need to pass a
                  "pseudo value" which can be used as an identifier to locate the real object.

                  So here is the process:
                  Locate the object you want to pass.

                  The "id" values do not exist at run time. You can not use it.

                  So you need a property that DOES exist at run time. Qt has defined the "objectName" attribute to all "normal" objects. Its default value is the empty string. You can set it to any value you want. You can use this value to allow the called function to locate your object. You can pass it to any function because it is only a string. You can even change it at execution time.

                  Have the called function use this value to locate the real object.

                  Be aware: The objectName is a "string" there is no guarantee that it is unique. If you create your own object (a separate qml file). In that file you set the objectName value (and don't change it) then all instances of the object will have the same objectName value. You have to come up with the method to identify multiple occurrences. Or you should make it clear that this is an assumption that your application is dependent on.

                  Hope this helps

                  G Offline
                  G Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on 23 Aug 2020, 13:57 last edited by GrecKo
                  #7

                  @lawrence-emke said in How do QML objects pass in function parameters?:

                  @Allon You should not pass QML objects as arguments in a function call.
                  All function calls are a "pass by value". This means that a "copy"
                  of your object is passed to your function (objects are not small things and have a lot of pointers). The copied object is not the real object.
                  Any changes you make will be to the copied object.

                  That is incorrect, you can pass QML objects as arguments to a function, and they aren't passed as value if they are QObjects, but as pointers. Plain JS objects do get passed as value though.

                  If you want to pass an object to a function you need to pass a
                  "pseudo value" which can be used as an identifier to locate the real object.
                  So here is the process:
                  Locate the object you want to pass.
                  The "id" values do not exist at run time. You can not use it.

                  You can use id at runtime, it's not a property but it can be used to identify an object.

                  Item {
                      id: foo
                  }
                  Item {
                      id: bar
                      function func(item) { ... }
                  }
                  
                  // calling bar.func(foo) works and bar can modify foo in func
                  

                  If you can do something else, don't rely on objectName to identify an object.

                  As for @Allon 's actual problem, it is just a syntactic error.

                  Instead of

                             function setRect(Object obj) { 
                                   anchors.fill: obj
                             }  
                  

                  You should have

                             function setRect(obj) { 
                                   anchors.fill = obj;
                             }  
                  

                  That code is correct but doesn't like good practice QML, too imperative. I would advice you to rethink what you are doing.

                  A 1 Reply Last reply 23 Aug 2020, 17:06
                  0
                  • G GrecKo
                    23 Aug 2020, 13:57

                    @lawrence-emke said in How do QML objects pass in function parameters?:

                    @Allon You should not pass QML objects as arguments in a function call.
                    All function calls are a "pass by value". This means that a "copy"
                    of your object is passed to your function (objects are not small things and have a lot of pointers). The copied object is not the real object.
                    Any changes you make will be to the copied object.

                    That is incorrect, you can pass QML objects as arguments to a function, and they aren't passed as value if they are QObjects, but as pointers. Plain JS objects do get passed as value though.

                    If you want to pass an object to a function you need to pass a
                    "pseudo value" which can be used as an identifier to locate the real object.
                    So here is the process:
                    Locate the object you want to pass.
                    The "id" values do not exist at run time. You can not use it.

                    You can use id at runtime, it's not a property but it can be used to identify an object.

                    Item {
                        id: foo
                    }
                    Item {
                        id: bar
                        function func(item) { ... }
                    }
                    
                    // calling bar.func(foo) works and bar can modify foo in func
                    

                    If you can do something else, don't rely on objectName to identify an object.

                    As for @Allon 's actual problem, it is just a syntactic error.

                    Instead of

                               function setRect(Object obj) { 
                                     anchors.fill: obj
                               }  
                    

                    You should have

                               function setRect(obj) { 
                                     anchors.fill = obj;
                               }  
                    

                    That code is correct but doesn't like good practice QML, too imperative. I would advice you to rethink what you are doing.

                    A Offline
                    A Offline
                    Allon
                    wrote on 23 Aug 2020, 17:06 last edited by
                    #8

                    @GrecKo I do not have any problem. This is mirro's one :) I just tryed to help.

                    1 Reply Last reply
                    0
                    • M mirro
                      22 Aug 2020, 02:24
                      Item{
                          id:root
                          property string btnTest:""
                         void setPos(Button curBtn)
                         {
                              btnTest = curBtn.text
                         }
                          Button{
                              id:btn
                              text:"TEST"
                          }
                      }
                      
                      L Offline
                      L Offline
                      lawrence.emke
                      wrote on 23 Aug 2020, 21:55 last edited by
                      #9

                      @mirro I stand corrected. My question is how does the QML engine know when to pass by reference and when to pass by value, since the syntax is the same? Are only the basic data types passed by value, and everything else is passed by reference? I thought I read in the documentation somewhere that it was not a good idea. I guess that has changed. Is there some documentation that I can read that explains this? Please let me know. I want to learn.

                      L 1 Reply Last reply 23 Aug 2020, 22:06
                      0
                      • L lawrence.emke
                        23 Aug 2020, 21:55

                        @mirro I stand corrected. My question is how does the QML engine know when to pass by reference and when to pass by value, since the syntax is the same? Are only the basic data types passed by value, and everything else is passed by reference? I thought I read in the documentation somewhere that it was not a good idea. I guess that has changed. Is there some documentation that I can read that explains this? Please let me know. I want to learn.

                        L Offline
                        L Offline
                        lawrence.emke
                        wrote on 23 Aug 2020, 22:06 last edited by
                        #10

                        @lawrence-emke
                        I just found the following documentation:

                        The QML engine has the ability to introspect QObject instances through the meta-object system. This means any QML code can access the following members of an instance of a QObject-derived class:

                        Properties
                        Methods (providing they are public slots or flagged with Q_INVOKABLE)
                        Signals
                        (Additionally, enums are available if they have been declared with Q_ENUMS. See Data Type Conversion Between QML and C++ for more details.)

                        In general, these are accessible from QML regardless of whether a QObject-derived class has been registered with the QML type system. However, if a class is to be used in a way that requires the engine to access additional type information — for example, if the class itself is to be used as a method parameter or property, or if one of its enum types is to be used in this way — then the class may need to be registered.

                        Also note that a number of the important concepts covered in this document are demonstrated in the Writing QML Extensions with C++ tutorial.

                        Data Type Handling and Ownership
                        Any data that is transferred from C++ to QML, whether as a property value, a method parameter or return value, or a signal parameter value, must be of a type that is supported by the QML engine.

                        By default, the engine supports a number of Qt C++ types and can automatically convert them as appropriately when used from QML. Additionally, C++ classes that are registered with the QML type system can be used as data types, as can their enums if appropriately registered. See Data Type Conversion Between QML and C++ for further information.

                        Additionally, data ownership rules are taken into consideration when data is transferred from C++ to QML. See Data Ownership for more details.

                        I notice it says properties, methods and signals. It does not state QML objects. What does that mean? (an oversight?)

                        L 1 Reply Last reply 24 Aug 2020, 00:20
                        0
                        • L lawrence.emke
                          23 Aug 2020, 22:06

                          @lawrence-emke
                          I just found the following documentation:

                          The QML engine has the ability to introspect QObject instances through the meta-object system. This means any QML code can access the following members of an instance of a QObject-derived class:

                          Properties
                          Methods (providing they are public slots or flagged with Q_INVOKABLE)
                          Signals
                          (Additionally, enums are available if they have been declared with Q_ENUMS. See Data Type Conversion Between QML and C++ for more details.)

                          In general, these are accessible from QML regardless of whether a QObject-derived class has been registered with the QML type system. However, if a class is to be used in a way that requires the engine to access additional type information — for example, if the class itself is to be used as a method parameter or property, or if one of its enum types is to be used in this way — then the class may need to be registered.

                          Also note that a number of the important concepts covered in this document are demonstrated in the Writing QML Extensions with C++ tutorial.

                          Data Type Handling and Ownership
                          Any data that is transferred from C++ to QML, whether as a property value, a method parameter or return value, or a signal parameter value, must be of a type that is supported by the QML engine.

                          By default, the engine supports a number of Qt C++ types and can automatically convert them as appropriately when used from QML. Additionally, C++ classes that are registered with the QML type system can be used as data types, as can their enums if appropriately registered. See Data Type Conversion Between QML and C++ for further information.

                          Additionally, data ownership rules are taken into consideration when data is transferred from C++ to QML. See Data Ownership for more details.

                          I notice it says properties, methods and signals. It does not state QML objects. What does that mean? (an oversight?)

                          L Offline
                          L Offline
                          lawrence.emke
                          wrote on 24 Aug 2020, 00:20 last edited by lawrence.emke
                          #11

                          @lawrence-emke

                          Certainly not all objects are passed by reference. If that was the case, then passing two variables to a function, depending on the way in which the function is written could modify one of the passed arguments unintentionally. It seems more reasonable to prevent unexpected changes to arguments of a function by using copied value.

                          This is probably why most languages have some way of indicating an argument is passed by reference and not by value. Not only does it save copying, but makes the function writer aware that the changes made to the object are made directly to the object rather than a copy.

                          1 Reply Last reply
                          0

                          10/11

                          23 Aug 2020, 22:06

                          • Login

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