1. 问题
qml
用数组定义的属性,如果属性的某个值改变,无法执行onXxxxChanged
函数。
举例:
Item{property var test1: [1,2]onTest1Changed: {//这里不执行console.log("out",test1)}Timer{interval: 1000running: truerepeat: trueonTriggered: {root.test1[0] = root.test1[0] + 1;console.log("changed", root.test1)}}
}
2. 解决方案
定义临时变量整个接收属性的值,修改后再将该临时变量重新整个赋值给原来的属性。
Item{id: rootproperty var test1: [1,2]onTest1Changed: {//执行console.log("out",test1)}Timer{interval: 1000running: truerepeat: trueonTriggered: {//关键代码var temp = root.test1;temp[0] = temp[0] + 1;root.test1 = temp;console.log("changed", root.test1)}}
}