5

如何在我的 qtquick 项目中查找Text元素的属性颜色和更改值? my.qml 文件中的内容。

Rectangle {
    width: 300
    height: 200

    Text {
        x: 12
        y: 34
        color:red
    }  
}
4

1 回答 1

6

您需要设置 objectName 属性,如下所示:

Rectangle {
    width: 300
    height: 200

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

现在您可以找到并访问元素和属性。
例如,我在 Text 元素中找到颜色并更改为绿色:

view = QDeclarativeView(QUrl('widget.qml'),parent = object)
property = QDeclarativeProperty(view.rootObject().findChild(QDeclarativeItem, name="text1"),"color")
property.write("green")
于 2013-02-18T16:23:05.760 回答