'this' pointer in QML
-
QML have parent for referencing parent element, but does it have this for referencing this element?
For example (Button is an imaginary element that has 'clicked' signal and 'color' property):
@Item {
Row {
Repeater {
Button {
onClicked: {
setColor(this)
}
}
}
}
function setColor(o) {
o.color = "red";
}
}@ -
I think it doesn't have 'this' (any kind of), you can "look for youself":http://qt.gitorious.org/qt/qtdeclarative/blobs/6492909d345681dbe54deecbb02307a96b977b6d/src/quick/items/qquickitem.h. I'd suggest using 'id' property.
@Item {
Row {
Repeater {
Button {
id: myButton // now identifiable
onClicked: {
setColor(myButton) // use id
}
}
}
}
function setColor(o) {
o.color = "red";
}
}@ -
Actually, the keyword "this" has specific meaning defined in binding expressions, however is undefined in signal handlers and other dynamic functions. See http://qt-project.org/doc/qt-5.0/qtqml/qtqml-javascript-hostenvironment.html for more information.