I just want my TextField to lose focus when i click on ScrollView, but the problem is scrollview does not accept the focus using Qt.ClickFocus.
我只想让我的Textfield在我点击ScrollView时失去焦点,但问题是ScrollView不接受使用Qt.ClickFocus的焦点。
import QtQuick
import QtQuick.Controls.Universal
ApplicationWindow {
visible: true
width: 600
height: 400
ScrollView {
anchors.fill: parent
focusPolicy: Qt.ClickFocus
TextField {
id: textField
implicitWidth: 400
implicitHeight: 50
}
}
}
I think there is something interfering the click event because when i use
focusPolicy: Qt.TabFocus
it works perfectly just fine.
我认为有一些东西干扰了Click事件,因为当我使用FocusPolicy:Qt.TabFocus时,它工作得很好。
I'm using Qt 6.5. Is this a freaking bug?
我用的是Qt 6.5。这是个该死的虫子吗?
[Update]
I just solve the problem using Stephen Quan answer
[更新版]我刚刚用权志刚解决了这个问题。
import QtQuick
import QtQuick.Controls.Universal
import QtQuick.Layouts
ApplicationWindow {
visible: true
width: 600
height: 400
ScrollView {
id: scrollView
anchors.fill: parent
ColumnLayout {
id: columnLayout
TextField {
Layout.preferredWidth: 400
}
}
TapHandler {
onTapped: columnLayout.forceActiveFocus()
}
}
}
I just used the columnLayout so I don't have to explicitly set contentWidth and contentHeight
我只使用了ColumnLayout,所以不必显式设置Content Width和Content Height
更多回答
优秀答案推荐
You can also use a placeholder Item
that's a child of the ScrollView
as well as install a TapHandler
for the ScrollView
that calls that Item
's forceActiveFocus()
:
您还可以使用ScrollView的子项占位符项,并为调用该项的forceActiveFocus()的ScrollView安装TapHandler:
ScrollView {
anchors.fill: parent
TextField {
id: textField
implicitWidth: 400
implicitHeight: 50
}
Item { id: placeholderItem }
TapHandler { onTapped: placeholderItem.forceActiveFocus() }
}
You can Try it Online!
你可以在网上试一下!
更多回答
Thanks!! But, is that a bug in ScrollView?
谢谢!!但是,这是ScrollView中的一个漏洞吗?
No. When TextField has focused, ScrollView has focused too. When you click on ScrollView, it's like, huh, the ScrollView already has focus so there's no need for it to do anything. So, the only way to steal focus from TextField is to declare another ScrollView child.
不是的。当Textfield聚焦时,ScrollView也聚焦了。当你点击ScrollView时,感觉就像,哈,ScrollView已经有焦点了,所以它不需要做任何事情。因此,从Textfield窃取焦点的唯一方法是声明另一个ScrollView子级。
Ohh. I see, that makes a lot of sense. Thanks man, you are freaking priceless!!
哦.。我明白了,这很有道理。谢谢你,你真是无价之宝!!
我是一名优秀的程序员,十分优秀!