What is the 'this' keyword in QML

Item {
    id: test
    Component.onCompleted: console.log("this is ", this, test)
}

The above code output shows the same pointer to Item, so what is a thiskeyword in QML? Is this a pointer to the nearest QML component and can be used in the same way as id?

+4
source share
1 answer

this is an attribute related to the QML object, but its scope is local and does not reach the scope of the children.

Instead

an object can be transferred idfrom anywhere within the scope of the component in which it is declared. Therefore, the identification value must always be unique within the scope of the component.

for example: In the following code, it is noted that this in the second element refers to point 2 not to element 1.

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)
        }
    }
}

Conclusion:

qml: item1
qml: true
qml: false
qml: item2
qml: false
qml: true
+9
source

Source: https://habr.com/ru/post/1694895/


All Articles