Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find property and change value in QtQuick

how to find property color and change value for Text element in my qtquick project?
content on my.qml file.

Rectangle {
    width: 300
    height: 200

    Text {
        x: 12
        y: 34
        color:red
    }  
}
like image 439
omid Avatar asked Feb 18 '13 15:02

omid


People also ask

How do you use property in QML?

A property is an attribute of an object that can be assigned a static value or bound to a dynamic expression. A property's value can be read by other objects. Generally it can also be modified by another object, unless a particular QML type has explicitly disallowed this for a specific property.

What is property binding in QML?

Property bindings are a core feature of QML that lets developers specify relationships between different object properties. When a property's dependencies change in value, the property is automatically updated according to the specified relationship.

What is property alias in QML?

Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).

What is QObject in QML?

The QtObject type is a non-visual element which contains only the objectName property. It can also be useful for C++ integration, as it is just a plain QObject.


1 Answers

you need to set objectName property like below:

Rectangle {
    width: 300
    height: 200

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

now you can find and access to element and property.
for example, i find color in Text element and change to green:

view = QDeclarativeView(QUrl('widget.qml'),parent = object)
property = QDeclarativeProperty(view.rootObject().findChild(QDeclarativeItem, name="text1"),"color")
property.write("green")
like image 139
punisher Avatar answered Oct 07 '22 02:10

punisher