我想知道是否有一种方法可以通过 var 选择具有特定id或objectName在 javascript 函数中已创建的 QML 对象(var 是与 QML 对象 ID 或名称相对应的字符串参数)。例如:
// main.qml
ApplicationWindow {
RoundButton {
id: btn1
}
RoundButton {
id: btn2
}
RoundButton {
id: btn3
}
// ...
function foo(qmlObjectNameOrId) {
qmlObjectNameOrId.text = "qmlObjectNameOrId is already in the document and has a property text that I want to set";
// Qt.findQmlObject(id) would have been great !
}
}
Run Code Online (Sandbox Code Playgroud)
这Qt.createQmlObject不是一个解决方案,因为我想使用已经创建的 QML 对象。
在C++中,实现这一点的方法是使用QQmlApplicationEngine对象,然后使用QML根对象通过QML对象名称执行选择:
// main.qml
ApplicationWindow {
RoundButton …Run Code Online (Sandbox Code Playgroud)