1 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
這是由于綁定引起的。每次執(zhí)行“testFocusDispatcher”時(shí),“currentId”都會(huì)改變,這將導(dǎo)致綁定重新評(píng)估,從而導(dǎo)致“testFocusDispatcher”執(zhí)行等......你看到了問(wèn)題!
相反,我會(huì)做這樣的事情:
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
}
}
}
添加回答
舉報(bào)