Access the properties of a child of a component
-
I have a component in qml and a rectangle as its child. How do I access the properties of that rectangle. If I try to access via id, I get an reference error. After some search I found that we need to use alias for the those properties. So I have changed the code to the following.
Component{ id:selectComponent Rectangle { id: selComp property alias width_rect: selComp.width } } Button{ .... onClicked: { console.log(selectionComponent.width_rect) } }
I stiil get an undefined error. How do I access the properties of the child rectangle?
Thanks!!
-
move the property alias outside the rectagle.
Component{ id:selectComponent Rectangle { id: selComp } property alias width_rect: selComp.width }
-
you should access the property via the property of that item , i.e selectionComponent.selComp.width_rect or selComp.width_rect .
@kumar90 said in Access the properties of a child of a component:
selectionComponent.selComp.width_rect
For this I get the error:
TypeError: Cannot read property 'width_rect' of undefined
Basically, It can't access the id of the child so it gives the undefined error!!
-
@saitej
Component
needs to be loaded first either byLoader
or using createObject. -
@saitej
Component
needs to be loaded first either byLoader
or using createObject.Ya ... I am already creating the object on a mouse click event.
selection = selectComponent.createObject(parent, {"x": parent.width / 4, "y": parent.height / 4, "width": parent.width / 2, "height": parent.width / 2})
I got it now. I was accessing via selectComponent rather than selection (object )
Thanks
-
@saitej
Then you need to access or set the properties byselection
object and notselectComponent
.