How to acess a subobject of dynamically created object
-
Hi,
I have a qml file called gasgroup.qml as below:
Rectangle{
id:gasgroup
Row{
id:row
}
Column{
id:col
}
}I then create a object from this qml dynamically
var gasobject = GasComponent.createobject(gasgrouop.qml);i want to access the rowobject(row) inside the gasgroup.qml from this dynamic object
gasobject.Can someone explain how can i do so ? -
Please use code tags around your code, it makes it much easier to read.
i want to access the rowobject(row) inside the gasgroup.qml from this dynamic object gasobject.Can someone explain how can i do so ?
There are a few ways to do it:
- add an alias property in
gasgroup
pointing to your subobject. Then you can easily access it usinggasobject.yourAliasProp
- name the subobject (
objectName
property), then use QObject'sfindChild()
method - use
children
property:gasobject.children[0]
The alias property is by far the most elegant option.
- add an alias property in
-
@sierdzio said in How to acess a subobject of dynamically created object:
meth
Thanks for replying!
I tries the property alias way by adding the below line to my "gasgroup.qml"
"property alias row: row".
I have a seperate JS file from which I am first creating a "gasgroupcomponent"
and then using createobject() to create the object.Even after adding property alias in qml file still I am getting the below error:
qrc:/GasGroupBuilder.js:112: TypeError: Cannot read property 'row' of undefined -
@anuj87_ said in How to acess a subobject of dynamically created object:
Cannot read property 'row' of undefined
That means that your object is undefined. So your gasobject is not set / created properly.
-
@sierdzio said in How to acess a subobject of dynamically created object:
name the subobject (objectName property), then use QObject's findChild() method
is findChild() object method accessible from JS. I did it this way.
Example:
GasGroupComponent = Qt.createComponent("GasGroup.qml")
gasGroupObject = GasGroupComponent .createObject(parent);
var rowobject = gasGroupObject.findChild("rowest");
if(rowobject !== null)
console.log("Row object found in GasGroup Object");
}But I am getting this error
"qrc:/GasGroupBuilder.js:52: TypeError: Property 'findChild' of object GasGroup_QMLTYPE_0(0x2d51d840) is not a function"gasGroup Object is getting created as i am able to see all the visual component after object creation.
-
Thanks sierdzio and yashpal
With your valuabl input I am able for create a soulution by adding a method "findchild( object_name)" inside gasgroup.qml
function findchildObject( object_name)
{
for( var i =0 ; i<gasgroup.children.length; i++)
{
if(gasgroup.children[i].objectName === object_name)
return gasgroup.children[i];
}
return null;
}I am able to get the childobjcet now by using the children and objectName property.
Thanks