Qml常见诡异失焦情况一般为明明设置了某一控件的焦点, 实际却是不生效。这到底时什么情况呢?用例子来分析这种情况。
Dialog {
id: dialog
width: 100; height: 100
onHidden: {
dialog.forceReset() // restore focus to 'yes' button
}
}
Rectangle {
width: 100; height: 200
color: activeFocus ? "red" : "lightblue"
MouseArea {
anchors.fill: parent
onClicked: {
parent.forceActiveFocus()
dialog.hide();
}
}
}
dialog.hide()
调用后dialog重新获得了焦点。Rectangle {
width: 100; height: 200
focus: true
color: activeFocus ? "red" : "lightblue"
MouseArea {
anchors.fill: parent
onClicked: parent.forceActiveFocus()
}
}
focus:true
间接地获得焦点了。FocusScope {
id: scope
width: 100; height: 200
focus: false
Rectangle {
id: rect
anchors.fill: parent
focus: true
color: activeFocus ? "red" : "lightblue"
}
}