Item {
id: test
Component.onCompleted: console.log("this is ", this, test)
}
The above code output shows the same pointer to the Item, so what is this
keyword in QML?
Is that the pointer of the nearest QML component and can be used same as id
?
this
is an attribute that refers to the QML object but its scope is local and does not reach the scope of the children.
Instead:
object can be referred by its
id
from anywhere within the scope of the component in which it is declared. Therefore, an identification value must always be unique within its component scope.
for example: In the following code it is observed that the this within the second Item refers to item2 not to item1.
Item{
id: item1
Component.onCompleted: {
console.log("item1")
console.log(this === item1)
console.log(this === item2)
}
Item{
id: item2
Component.onCompleted: {
console.log("item2")
console.log(this === item1)
console.log(this === item2)
}
}
}
Output:
qml: item1
qml: true
qml: false
qml: item2
qml: false
qml: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With