我正在使用Qt 5.8(vs 2015 32 bit)。在未连接物理键盘/鼠标的Surface Pro平板电脑上运行QML应用程序时,无论我当前是否在尝试输入文本框,键盘都会弹出。更改文本父级的可见性不会关闭键盘。下次用户触摸应用程序时,它将弹出。如果应用程序处于全屏模式,则键盘会在应用程序后面弹出,但键盘仍可单击,从而无法单击应用程序本身。有什么建议如何按需编程显示/隐藏键盘?
此示例代码为我生成了键盘弹出窗口。我曾尝试将文本字段的焦点更改为false(如果用户单击Enter或父窗口不再可见,但这无效)。当父窗口不再可见时,我也尝试使用Qt.inputMethod.hide(),但这无效。
main.qml
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property bool showPage1: false
MyPage1 {
id: page1
visible: showPage1
width: 1/2 * parent.width
height: parent.height /3
anchors.left: parent.left
Rectangle {
width: 50
height: 50
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.margins: 10
color: "yellow"
MouseArea {
anchors.fill: parent
onClicked: console.log("page 1 corner clicked")
}
}
}
Rectangle {
width: 50
height: 50
color: "blue"
anchors.left: parent.left
anchors.bottom: parent.bottom
anchors.margins: 10
MouseArea {
anchors.fill: parent
onClicked: {
showPage1 = !showPage1
}
}
}
}
MyPage1.qml
import QtQuick.Controls 1.1
Rectangle {
id: page1Root
color: "pink"
border.color: "red"
border.width: 4
anchors.fill: parent
Text {
text: "Page 1!"
anchors.fill: parent
anchors.top: parent.top
anchors.topMargin: 10
width: parent.width * 3/4
height: 30
horizontalAlignment: Text.AlignHCenter
}
TextField {
id: myTextField
width: parent.width * 3/4
height: 50
anchors.centerIn: parent
text: "type something here"
onTextChanged: {
console.log("text changed to: " + text)
}
horizontalAlignment: TextEdit.AlignLeft
verticalAlignment: TextEdit.AlignVCenter
font.pixelSize: 22
// Keys.onReturnPressed: {
// focus = false
//// Qt.inputMethod.hide()
// }
onFocusChanged: console.log("focus changed: " + focus)
// MouseArea {
// anchors.fill: parent
// onClicked: {
// console.log("text field clicked!")
// focus = true
// }
// }
}
onVisibleChanged: {
console.log("page 1 root visible: " + visible)
if (visible)
{
myTextField.forceActiveFocus()
}
else
{
myTextField.focus = false
// Qt.inputMethod.hide()
}
}
}
在MyPage1.qml中,我正在侦听文本更改,以查看页面不可见以及键盘上的用户类型是否更改了文本,但没有看到任何文本更改。
事实证明TextInput不会调出键盘,因此我将使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句