请问,是否可以从javascript函数访问和更改qml元素的属性?我读到了document.getElementById()应该做的伎俩,但当我尝试时,我得到:
ReferenceError:未定义文档
Test.qml
import QtQuick 2.0
import QtQuick.Controls 1.2
import Test.js as JS
Rectangle{
id: rect
color: "red"
onClicked: {
JS.changeMe();
}
}
Test.js
function changeMe(){
//change the color of element "rect"
//document.getElementById("rect")
}
答案 0 :(得分:1)
这有效:
Rectangle{
id: rect
color: "red"
onClicked: {
JS.changeMe(rect);
}
}
function changeMe(rect){
rect.color = "yellow";
}