Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'this' keyword in QML

Tags:

qt

qt5

qml

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?

like image 441
JustWe Avatar asked Mar 15 '18 05:03

JustWe


1 Answers

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
like image 85
eyllanesc Avatar answered Oct 22 '22 05:10

eyllanesc