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. Pass object like property into QML Component. How?
Forum Updated to NodeBB v4.3 + New Features

Pass object like property into QML Component. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.7k 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    Hello all!
    Is there option to pass object into QML Component? For example:

    CustomItem.qml

    CustomItem {
        property SomeType object: objectID;
        ...
        Component.onCompleted: {
            objectID.someMethod();
        }
    }
    

    any other qml where used CustomItem.qml

    ...
    Item {
        id: AnyObject;
        ...
    }
    
    CustomItem {
        object: AnyObject;
    }
    

    The use-case for it looks like - ability to define any other object by ID inside of QML from outside through property. Is it possible?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bogong
      wrote on last edited by bogong
      #2

      Solution found.

      CustomItem.qml

      CustomItem {
          property var object;
          ...
          Component.onCompleted: {
              if (object) {object.someMethod();}
          }
      }
      

      any other *.qml

      ...
      Item {
          id: AnyObject;
          ...
      }
      
      CustomItem {
          object: AnyObject;
      }
      
      1 Reply Last reply
      0
      • IntruderExcluderI Offline
        IntruderExcluderI Offline
        IntruderExcluder
        wrote on last edited by
        #3

        @bogong said in Pass object like property into QML Component. How?:

        Is it possible?

        Short answer is "Yes". But you should be sure in your case if object property is valid and contains someMethod. Simple example:

        ApplicationWindow {
            id: root
            visible: true
            width: 400
            height: 680
        
            Item {
                id: item
                function a() { console.log("a"); }
            }
        
            Item {
                id: item2
                property Item customprop: null
        
                onCustompropChanged: {
                    console.log("customprop changed");
                    customprop.a();
                }
            }
        
            Component.onCompleted: {
                item2.customprop = item
            }
        }
        
        B 2 Replies Last reply
        0
        • IntruderExcluderI IntruderExcluder

          @bogong said in Pass object like property into QML Component. How?:

          Is it possible?

          Short answer is "Yes". But you should be sure in your case if object property is valid and contains someMethod. Simple example:

          ApplicationWindow {
              id: root
              visible: true
              width: 400
              height: 680
          
              Item {
                  id: item
                  function a() { console.log("a"); }
              }
          
              Item {
                  id: item2
                  property Item customprop: null
          
                  onCustompropChanged: {
                      console.log("customprop changed");
                      customprop.a();
                  }
              }
          
              Component.onCompleted: {
                  item2.customprop = item
              }
          }
          
          B Offline
          B Offline
          bogong
          wrote on last edited by
          #4

          @intruderexcluder Just replied before you in seconds ...

          1 Reply Last reply
          0
          • IntruderExcluderI IntruderExcluder

            @bogong said in Pass object like property into QML Component. How?:

            Is it possible?

            Short answer is "Yes". But you should be sure in your case if object property is valid and contains someMethod. Simple example:

            ApplicationWindow {
                id: root
                visible: true
                width: 400
                height: 680
            
                Item {
                    id: item
                    function a() { console.log("a"); }
                }
            
                Item {
                    id: item2
                    property Item customprop: null
            
                    onCustompropChanged: {
                        console.log("customprop changed");
                        customprop.a();
                    }
                }
            
                Component.onCompleted: {
                    item2.customprop = item
                }
            }
            
            B Offline
            B Offline
            bogong
            wrote on last edited by bogong
            #5

            @intruderexcluder said in Pass object like property into QML Component. How?:

            you should be sure in your case if object property is valid and contains someMethod

            I understand it because there are no protocol or interface functionality in QML.

            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