QML 中的 forceActiveFocus() 与 focus = true [英] forceActiveFocus() vs focus = true in QML
问题描述
我阅读了有关以下内容的文档:
I read the documentation about:
focus
propertyactiveFocus
propertyforceActiveFocus()
methodFocusScope
object- and keyboard focus in QtQuick
但仍不清楚何时应该使用 forceActiveFocus()
方法而不是将 focus
属性设置为 true,反之亦然.
but it is still not clear when should someone use forceActiveFocus()
method over setting the focus
property to true or vice versa.
推荐答案
作为文档状态:
对于非常简单的情况,有时只需设置焦点属性就足够了.
For very simple cases simply setting the focus property is sometimes sufficient.
这样一个简单的情况是,如果获得 focus: true
的 Item
没有被可能没有重点.
Such a simple case would be, if the Item
which gets focus: true
is not enclosed by a FocusScope
that might have not the focus.
然后继续:
>在每个焦点范围内,一个对象可能会将 Item::focus 设置为 true.如果多个 Item 设置了焦点属性,则设置焦点的最后一个类型将具有焦点,而其他类型则未设置,类似于没有焦点范围的情况.
> Within each focus scope one object may have Item::focus set to true. If more than one Item has the focus property set, the last type to set the focus will have the focus and the others are unset, similar to when there are no focus scopes.
>当焦点范围接收到活动焦点时,设置了焦点的包含类型(如果有)也将获得活动焦点.如果此类型也是 FocusScope,则代理行为将继续.焦点范围和子焦点项都将设置 activeFocus 属性.
> When a focus scope receives active focus, the contained type with focus set (if any) also gets the active focus. If this type is also a FocusScope, the proxying behavior continues. Both the focus scope and the sub-focused item will have the activeFocus property set.
从我们了解到,设置 focus: true
是不够的,如果它是 Item
是 FocusScope 的继承者
因为这个 FocusScope
需要有 activeFocus
su 以便后续 Item
将接收 activeFocus
.这是递归的,意味着 FocusScope
需要有 focus: true
并且可能的前身 FocusScope
需要 activeFocus
等等.这会导致某种焦点树
From where we learn, about the fact that setting focus: true
is not sufficient, if it is for an Item
that is a successor to a FocusScope
as this FocusScope
would need to have activeFocus
su that the successor Item
will receive activeFocus
. This is recursive, and means, that the FocusScope
will need to have focus: true
and a possible predecessor FocusScope
needs the activeFocus
and so on. Which results in some kind of focus tree
这个焦点树由内部节点组成,它们是FocusScope
和叶子,它们是项目
s.FocusScope
也可能是 leaf,但我不知道为什么会这样.
This focus tree consists out of inner nodes that are the FocusScope
s and leafs that are Item
s. A FocusScope
might be a leaf as well, but I don't know why it should.
在这棵树中,每个 FocusScope
最多可以有一个 子节点(Item
(leaf)或具有 focus === true
的 FocusScope
(inner node).遍历这棵树,遵循所有遍历节点具有 的路径focus === true
遍历的节点也有 activeFocus === true
.因为每个 FocusScope
最多只能有一个 子节点 focus === true
只有一个这样的路径.
In this tree, each FocusScope
may have up to one child node (either Item
(leaf) or FocusScope
(inner node) that has focus === true
. Traversing this tree, following the path where all traversed nodes have focus === true
the traversed nodes have also activeFocus === true
. As each FocusScope
may have only up to one child node with focus === true
there is only one such path.
Column {
FocusScope {
focus: false
width: 100
height: 100
Text {
focus: true
text: 'has focus ' + focus + '
has activeFocus ' + activeFocus
}
}
FocusScope {
focus: true
width: 100
height: 100
Text {
focus: true
text: 'has focus ' + focus + '
has activeFocus ' + activeFocus
}
}
}
这里有两个 FocusScope
.两者都有一个具有 focus
的孩子,但由于只有第二个 FocusScope
本身具有 focus
,因此它的孩子具有 activeFocus
.
Here we have two FocusScope
s. Both have a child that has focus
, but as only the second FocusScope
has the focus
itself, it's child has activeFocus
.
forceActiveFocus()
的使用遍历焦点树,为途中的每个节点设置focus
为true
,所以Item
末尾有 activeFocus
.
The use of forceActiveFocus()
traverses the focus tree, and sets focus
to true
for each node on the way, so the Item
has activeFocus
at the end.
这篇关于QML 中的 forceActiveFocus() 与 focus = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!