Solved Pass object like property into QML Component. How?
-
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?
-
Solution found.
CustomItem.qml
CustomItem { property var object; ... Component.onCompleted: { if (object) {object.someMethod();} } }
any other *.qml
... Item { id: AnyObject; ... } CustomItem { object: AnyObject; }
-
@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 containssomeMethod
. 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 } }
-
@intruderexcluder Just replied before you in seconds ...
-
@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.