如何在QtQuick中查找属性并修改值

5 投票
1 回答
1303 浏览
提问于 2025-04-17 16:16

如何在我的qtquick项目中找到color属性,并更改Text元素的值?
这是我.qml文件中的内容。

Rectangle {
    width: 300
    height: 200

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

1 个回答

6

你需要像下面这样设置对象名称属性:

Rectangle {
    width: 300
    height: 200

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

现在你可以找到并访问这个元素和它的属性了。
比如,我在文本元素中找到了颜色属性,并把它改成了绿色:

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

撰写回答