英文:
How to loose focus when i click outside TextInput in QML
问题
我在Column
中有三个元素,即Button
、Rectangle
和ListView
。TextInput
是Rectangle
的子元素。当在ListView
中选择/点击任何项目时,光标仍然可见在TextInput
中。我希望只有在编辑TextInput
时才能使用光标,其他情况下不可见。
为了简单起见,我删除了不必要的代码。
Column {
anchors.fill: parent
Button {
id: backButton
onClicked: { . . . }
}
Rectangle {
id: myRect
TextInput {
id: inputText
anchors {
fill: parent
}
cursorVisible: false
onAccepted: { // do something}
}
}
ListView {
.
.
}
}
英文:
I have three elements inside Column
i.e. Button
, Rectangle
and ListView
. TextInput
is the child of Rectangle
. When select/click on any item in ListView
, the cursor is still visible in TextInput
. I want the cursor to be available ONLY when i'm editing the TextInput
and not otherwise.
For simplicity sake, i have removed unnecessary code.
Column {
anchors.fill: parent
Button {
id: backButton
onClicked: { . . . }
}
Rectangle {
id: myRect
TextInput {
id: inputText
anchors {
fill: parent
}
cursorVisible: false
onAccepted: { // do something}
}
}
ListView {
.
.
}
}
答案1
得分: 1
你可以在某个地方放置一个MouseArea来捕捉外部的鼠标点击事件,并将焦点从TextInput移开。类似下面的代码应该可以工作:
Column {
Button {
}
Rectangle {
TextInput {
}
}
ListView {
id: listView
MouseArea {
anchors.fill: parent
onClicked: listView.forceActiveFocus();
}
}
}
英文:
You can simply put a MouseArea somewhere to catch the outside mouse clicks and move the focus away from the TextInput. Something like this should work:
Column {
Button {
}
Rectangle {
TextInput {
}
}
ListView {
id: listView
MouseArea {
anchors.fill: parent
onClicked: listView.forceActiveFocus();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论