1 回答

TA贡献1943条经验 获得超7个赞
这是由于绑定引起的。每次执行“testFocusDispatcher”时,“currentId”都会改变,这将导致绑定重新评估,从而导致“testFocusDispatcher”执行等......你看到了问题!
相反,我会做这样的事情:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
Window {
width: 640
height: 480
visible: true
title: qsTr("Focus sandbox")
Grid {
id: grid
columns: 2
spacing: 2
property variant ids: [topLeft,topRight,bottomLeft,bottomRight]
property variant currentId: 0
Keys.onTabPressed: {
console.log("Current id: " + grid.currentId)
if(grid.currentId < 3) {
grid.currentId++;
}
else {
grid.currentId=0;
}
}
Rectangle {
id: topLeft
width: 50; height: 50
color: focus ? "red" : "lightgray"
focus: grid.ids[grid.currentId] === this
}
Rectangle {
id: topRight
width: 50; height: 50
color: focus ? "red" : "lightgray"
focus: grid.ids[grid.currentId] === this
}
Rectangle {
id: bottomLeft
width: 50; height: 50
color: focus ? "red" : "lightgray"
focus: grid.ids[grid.currentId] === this
}
Rectangle {
id: bottomRight
width: 50; height: 50
color: focus ? "red" : "lightgray"
focus: grid.ids[grid.currentId] === this
}
}
}
添加回答
举报