How to access item's children
-
Hi,
For instance, the rect.color. How do I access it?
Thanks!
@
//SimpleRect.qml
Item
{
width:100
height:100Rectangle
{
id:Rect
anchors.fill: parent
color: "green"}
}
@ -
From the "doc":http://developer.qt.nokia.com/doc/qt-4.7/qdeclarativeintroduction.html#id-0072e7d8-628c-49ee-810f-35f1b672c46f:
[quote]Note that an id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.[/quote]@//SimpleRect.qml
Item {
width:100
height:100Rectangle {
id: myrectangle
anchors.fill: parent
color: "green"
}}@
and you acces it with myrectangle.color -
Thanks for the reply.
What if I want to use SimpleRect in another qml? How do I access the color property then?
@
Rectangle
{
color: "pink"
width:500
height:500SimpleRect
{
width:100
height:100
x: 200
y:200
rect.color : "blue" // How do I access the color then?
}}@
-
You have to make alias property or bind parent property with child property:
Alias:
@
//SimpleRect.qml
Item {
id: root
...
property alias childColor: myrectangle.colorRectangle {
id: myrectangle
...
}
}
@Binding:
@
//SimpleRect.qml
Item {
id: root
...
property color childColorRectangle {
id: myrectangle
...
color: root.childColor
}
}
@Usage in both cases:
@
//OtherFile.qml
SimpleRect {
id: simple
...
childColor: "blue"
}
@ -
[quote author="michal.k" date="1312787834"]
Binding:
@
//SimpleRect.qml
Item {
id: root
...
property color childColorRectangle {
id: myrectangle
...
color: root.childColor
}
}
@[/quote]The best is use alias, not binding. With above code, when you change color of myrectangle, childColor is not updated.
Alias resolves this problem.