QT 5.10 文档中关于 Qt Quick 键盘焦点的错误?
A bug about Keyboard Focus in Qt Quick in QT 5.10 document?
我正在阅读 Qt 文档以学习 Qt Quick 中的键盘焦点!我运行 文档中的代码。但是,结果与文档不同!代码如下
main.qml
//Window code that imports MyWidget
Rectangle {
id: window
color: "white"; width: 240; height: 150
Column {
anchors.centerIn: parent; spacing: 15
MyWidget {
focus: true //set this MyWidget to receive the focus
color: "lightblue"
}
MyWidget {
color: "palegreen"
}
}
MyWidget.qml
Rectangle {
id: widget
color: "lightsteelblue"; width: 175; height: 25; radius: 10; antialiasing:
true
Text { id: label; anchors.centerIn: parent}
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_A)
label.text = 'Key A was pressed'
else if (event.key == Qt.Key_B)
label.text = 'Key B was pressed'
else if (event.key == Qt.Key_C)
label.text = 'Key C was pressed'
}
}
pic1是结果表格文档。 pic2 是我 运行ning 的结果,我只是从文档中复制代码并 运行 它。
图1
图2
这是一个错误吗?为什么结果不同?
医生说:
我们希望第一个 MyWidget 对象获得焦点,因此我们将其焦点 属性 设置为 true。但是,通过 运行ning 代码,我们可以确认第二个小部件获得了焦点。
查看 MyWidget 和 window 代码,问题很明显 - 有三种类型将焦点 属性 设置为 true。两个 MyWidgets 将焦点设置为 true,window 组件也设置了焦点。最终,只有一种类型可以拥有键盘焦点,系统必须决定哪种类型获得焦点。当创建第二个 MyWidget 时,它获得焦点,因为它是最后一个将其焦点设置为 属性 的类型。
我的问题:
1.Why 结果不同?在我的结果中,第一个小部件获得焦点。
2.Also,文档中的"because it is the last type to set its focus property to true"是什么意思?
您可以从这里获取文档!
Qt doc
我测试了很多次。结果是一样的。也许文档已经过时了!
当我 运行 代码时,我得到的结果与你相同,但这并不重要。这里的要点是要理解,如果没有 FocusScope
,您将无法控制哪个对象从系统获得焦点(您无法保证 QML 对象的创建顺序)。
您是否尝试在第二个 MyWidget
上设置 focus: true
?如果您尝试,键盘事件仍将由第一个小部件接收,除非您使用 FocusScope
.
请注意,您也可以从 MyWidget.qml 中删除 focus: true
,您甚至不需要手动添加 FocusScope
(它将由应用程序自动管理 window).
尝试 运行 并进行以下修改:
MyWidget { //the focus is here
color: "palegreen"
}
MyWidget {
focus: true //set this MyWidget to receive the focus
color: "lightblue"
}
MyWidget {
color: "palegreen"
}
我正在阅读 Qt 文档以学习 Qt Quick 中的键盘焦点!我运行 文档中的代码。但是,结果与文档不同!代码如下
main.qml
//Window code that imports MyWidget
Rectangle {
id: window
color: "white"; width: 240; height: 150
Column {
anchors.centerIn: parent; spacing: 15
MyWidget {
focus: true //set this MyWidget to receive the focus
color: "lightblue"
}
MyWidget {
color: "palegreen"
}
}
MyWidget.qml
Rectangle {
id: widget
color: "lightsteelblue"; width: 175; height: 25; radius: 10; antialiasing:
true
Text { id: label; anchors.centerIn: parent}
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_A)
label.text = 'Key A was pressed'
else if (event.key == Qt.Key_B)
label.text = 'Key B was pressed'
else if (event.key == Qt.Key_C)
label.text = 'Key C was pressed'
}
}
pic1是结果表格文档。 pic2 是我 运行ning 的结果,我只是从文档中复制代码并 运行 它。
图1
图2
这是一个错误吗?为什么结果不同?
医生说:
我们希望第一个 MyWidget 对象获得焦点,因此我们将其焦点 属性 设置为 true。但是,通过 运行ning 代码,我们可以确认第二个小部件获得了焦点。
查看 MyWidget 和 window 代码,问题很明显 - 有三种类型将焦点 属性 设置为 true。两个 MyWidgets 将焦点设置为 true,window 组件也设置了焦点。最终,只有一种类型可以拥有键盘焦点,系统必须决定哪种类型获得焦点。当创建第二个 MyWidget 时,它获得焦点,因为它是最后一个将其焦点设置为 属性 的类型。
我的问题:
1.Why 结果不同?在我的结果中,第一个小部件获得焦点。
2.Also,文档中的"because it is the last type to set its focus property to true"是什么意思?
您可以从这里获取文档! Qt doc
我测试了很多次。结果是一样的。也许文档已经过时了!
当我 运行 代码时,我得到的结果与你相同,但这并不重要。这里的要点是要理解,如果没有 FocusScope
,您将无法控制哪个对象从系统获得焦点(您无法保证 QML 对象的创建顺序)。
您是否尝试在第二个 MyWidget
上设置 focus: true
?如果您尝试,键盘事件仍将由第一个小部件接收,除非您使用 FocusScope
.
请注意,您也可以从 MyWidget.qml 中删除 focus: true
,您甚至不需要手动添加 FocusScope
(它将由应用程序自动管理 window).
尝试 运行 并进行以下修改:
MyWidget { //the focus is here
color: "palegreen"
}
MyWidget {
focus: true //set this MyWidget to receive the focus
color: "lightblue"
}
MyWidget {
color: "palegreen"
}