QtQuick.Controls 2.12 按钮不显示焦点
QtQuick.Controls 2.12 does not show focus on buttons
我正在使用 QtQuick.Controls 2.12 并且我正在尝试使用 3 个按钮显示 window,其中一个按钮将具有焦点。使用 QtQuick.Controls 1.4 一切都很好并且 Button1 有焦点(蓝色边框):
但是 QtQuick.Controls 2.12 结果是这样的:
如果我按 Tab 焦点将传递到 Button2、Button3、Button1...
我也尝试过强制聚焦,但没有结果。
这是main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
RowLayout{
anchors.fill: parent
Button{
id: button1
text: "Button1"
focus: true
}
Button{
text: "Button2"
onClicked: {
button1.forceActiveFocus()
}
}
Button{
text: "Button3"
}
}
}
我正在尝试在 window 启动时获取此信息:
允许高亮显示的属性是visualFocus
,如果文档被审核:
This property holds whether the control has visual focus. This
property is true when the control has active focus and the focus
reason is either Qt.TabFocusReason, Qt.BacktabFocusReason, or
Qt.ShortcutFocusReason.
In general, for visualizing key focus, this property is preferred over
Item::activeFocus. This ensures that key focus is only visualized when
interacting with keys - not when interacting via touch or mouse.
See also focusReason and Item::activeFocus.
(强调我的)
所以你必须使用 forceActiveFocus()
但传递指定的原因之一,因为如果你不传递参数,原因是 Qt::OtherFocusReason 正如文档指出的那样:
This is an overloaded function.
Forces active focus on the item with the given reason.
This method sets focus on the item and ensures that all ancestor
FocusScope objects in the object hierarchy are also given focus.
This method was introduced in Qt 5.1.
See also activeFocus and Qt::FocusReason.
Forces active focus on the item.
This method sets focus on the item and ensures that all ancestor
FocusScope objects in the object hierarchy are also given focus.
The reason for the focus change will be Qt::OtherFocusReason. Use the
overloaded method to specify the focus reason to enable better
handling of the focus change.
See also activeFocus.
(强调我的)
所以解决方案是:
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
RowLayout{
anchors.fill: parent
Button{
id: button1
text: "Button1"
}
Button{
text: "Button2"
}
Button{
text: "Button3"
}
}
Component.onCompleted: <b>button1.forceActiveFocus(Qt.TabFocusReason)</b>
}
看起来这只适用于 QML。
(https://doc.qt.io/qt-5/qstyle.html#developing-style-aware-custom-widgets)
我正在使用 QtQuick.Controls 2.12 并且我正在尝试使用 3 个按钮显示 window,其中一个按钮将具有焦点。使用 QtQuick.Controls 1.4 一切都很好并且 Button1 有焦点(蓝色边框):
但是 QtQuick.Controls 2.12 结果是这样的:
如果我按 Tab 焦点将传递到 Button2、Button3、Button1...
我也尝试过强制聚焦,但没有结果。
这是main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
RowLayout{
anchors.fill: parent
Button{
id: button1
text: "Button1"
focus: true
}
Button{
text: "Button2"
onClicked: {
button1.forceActiveFocus()
}
}
Button{
text: "Button3"
}
}
}
我正在尝试在 window 启动时获取此信息:
允许高亮显示的属性是visualFocus
,如果文档被审核:
This property holds whether the control has visual focus. This property is true when the control has active focus and the focus reason is either Qt.TabFocusReason, Qt.BacktabFocusReason, or Qt.ShortcutFocusReason.
In general, for visualizing key focus, this property is preferred over Item::activeFocus. This ensures that key focus is only visualized when interacting with keys - not when interacting via touch or mouse.
See also focusReason and Item::activeFocus.
(强调我的)
所以你必须使用 forceActiveFocus()
但传递指定的原因之一,因为如果你不传递参数,原因是 Qt::OtherFocusReason 正如文档指出的那样:
This is an overloaded function.
Forces active focus on the item with the given reason.
This method sets focus on the item and ensures that all ancestor FocusScope objects in the object hierarchy are also given focus.
This method was introduced in Qt 5.1.
See also activeFocus and Qt::FocusReason.
Forces active focus on the item.
This method sets focus on the item and ensures that all ancestor FocusScope objects in the object hierarchy are also given focus.
The reason for the focus change will be Qt::OtherFocusReason. Use the overloaded method to specify the focus reason to enable better handling of the focus change.
See also activeFocus.
(强调我的)
所以解决方案是:
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
RowLayout{
anchors.fill: parent
Button{
id: button1
text: "Button1"
}
Button{
text: "Button2"
}
Button{
text: "Button3"
}
}
Component.onCompleted: <b>button1.forceActiveFocus(Qt.TabFocusReason)</b>
}
看起来这只适用于 QML。
(https://doc.qt.io/qt-5/qstyle.html#developing-style-aware-custom-widgets)