How to find a property and change a value in QtQuick

How to find the color property and change the value for a Text element in my qtquick project?
the contents of the my.qml file.

Rectangle {
    width: 300
    height: 200

    Text {
        x: 12
        y: 34
        color:red
    }  
}
+5
source share
1 answer

you need to set the objectName property as shown below:

Rectangle {
    width: 300
    height: 200

    Text {
      objectName: "text1"  
               x: 12
               y: 34
               color: "red"
    }  
}

Now you can find and access elements and properties.
for example, I find the color in a text element and change it to green:

view = QDeclarativeView(QUrl('widget.qml'),parent = object)
property = QDeclarativeProperty(view.rootObject().findChild(QDeclarativeItem, name="text1"),"color")
property.write("green")
+6
source

All Articles