'this' pointer in QML
-
wrote on 5 Apr 2013, 13:38 last edited by
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";
}
}@ -
wrote on 5 Apr 2013, 14:10 last edited by
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";
}
}@ -
wrote on 8 Apr 2013, 00:02 last edited by
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.
1/3