我正在开发Qt快速应用程序,我想打开对话框。在这个对话框窗口中有一个TextField
,我想在dialog
打开后将焦点设置到这个textField
上。
这段代码不起作用。
function newFolder() {
newFolderDialog.visible = true
newFolderDialog.open()
}
Dialog {
id: newFolderDialog
title: "New folder"
height: 150
width: 300
standardButtons: StandardButton.Ok | StandardButton.Cancel
Column {
anchors.fill: parent
Text {
text: "Name"
height: 40
}
TextField {
id: newFolderInput
width: parent.width * 0.75
focus: true
onFocusChanged: console.log("Focus changed " + focus)
}
}
onVisibilityChanged: {
if(visible === true){
newFolderInput.text = ""
newFolderInput.focus = true
}
}
}
输出到控制台的内容是:
qml: 焦点变为 false
qml: 焦点变为 true
qml: 焦点变为 false
看起来,在我将焦点设置到 textField
后,焦点发生了某种变化。