use*_*372 6 dialog focus textfield qml qt-quick
我正在研究qt快速应用程序,我想打开对话框.在这个对话框窗口是TextField,我想在对话框打开后将焦点设置到这个textfiel.这段代码可以完成工作.
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出到控制台是
qml:焦点改变了假
qml:焦点改变了真
qml:焦点改变了false
看起来,在我将焦点设置为textField之后,某种程度上焦点会发生变化
您不需要写入的功能.来自Dialog函数的文档open():
显示用户的对话框.它相当于将visible设置为true.
鉴于(这不是问题),似乎焦点在对话框和包含的元素之间不断争用.打开/关闭Dialog的次数越多,评估就越多.我现在无法弄清楚为什么会这样.但是,你可以通过(1)摆脱onVisibilityChanged处理程序和(2)重写来轻松解决问题newFolder().最终代码重写:
ApplicationWindow {
width: 360
height: 300
visible: true
Button {
anchors.centerIn: parent
text: "click me!"
onClicked: newFolder()
}
Dialog {
id: newFolderDialog
title: "New folder"
height: 150
width: 300
standardButtons: StandardButton.Ok | StandardButton.Cancel
focus: true // Needed in 5.9+ or this code is NOT going to work!!
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)
}
}
}
function newFolder() {
newFolderDialog.open()
newFolderInput.focus = true
}
}
Run Code Online (Sandbox Code Playgroud)
这样,您首先打开对话框,然后将焦点设置为正确Item.
| 归档时间: |
|
| 查看次数: |
6398 次 |
| 最近记录: |