简体   繁体   English

在QML中forceActiveFocus()vs focus = true

[英]forceActiveFocus() vs focus = true in QML

I read the documentation about: 我阅读了有关以下内容的文档:

but it is still not clear when should someone use forceActiveFocus() method over setting the focus property to true or vice versa. 但尚不清楚何时应使用forceActiveFocus()方法将focus属性设置为true,反之亦然。

As the documentation states : 文档所述

For very simple cases simply setting the focus property is sometimes sufficient. 对于非常简单的情况,只需设置focus属性有时就足够了。

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. 如果获得focus: trueItem没有被可能没有焦点的FocusScope包围,则将是这种简单情况。

Then it continues with: 然后继续:

> Within each focus scope one object may have Item::focus set to true. >在每个焦点范围内,一个对象可能将Item :: focus设置为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. 如果设置了多个焦点属性,则最后一个设置焦点的类型将具有焦点,而其他类型则未设置,这与没有焦点作用域时类似。

> 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. 如果此类型也是FocusScope,则代理行为将继续。 Both the focus scope and the sub-focused item will have the activeFocus property set. 焦点范围和子焦点项都将设置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 . 从我们的学习中学到的事实是,如果focus: truefocus: true ,那么将focus: true设置为focus: true是不够的,因为对于FocusScope的后继Item ,此FocusScope将需要activeFocus ,因为后继Item将收到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. 这是递归的,这意味着FocusScope将需要具有focus: true并且可能的前一个FocusScope需要activeFocus等。 Which results in some kind of focus tree 导致某种焦点树

This focus tree consists out of inner nodes that are the FocusScope s and leafs that are Item s. 焦点树由作为FocusScope内部节点和作为Item叶子组成 A FocusScope might be a leaf as well, but I don't know why it should. FocusScope也可能是一片叶子 ,但我不知道为什么要这么做。

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. 在这棵树中,每个FocusScope可以有一个具有focus === true 子节点Item叶子 )或FocusScope内部节点 )),沿着所有经过的节点都具有focus === true的路径遍历该树。遍历的节点也具有activeFocus === true ,因为每个FocusScope最多只能有一个具有focus === true 子节点 ,因此只有一条这样的路径。

Column {
    FocusScope {
        focus: false
        width: 100
        height: 100
        Text {
            focus: true
            text: 'has focus ' + focus + '\nhas activeFocus ' + activeFocus
        }
    }
    FocusScope {
        focus: true
        width: 100
        height: 100
        Text {
            focus: true
            text: 'has focus ' + focus + '\nhas activeFocus ' + activeFocus
        }
    }
}

Here we have two FocusScope s. 这里有两个FocusScope Both have a child that has focus , but as only the second FocusScope has the focus itself, it's child has activeFocus . 两者都有一个具有focus的子对象,但是由于只有第二个FocusScope本身具有focus ,因此它的子对象具有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. 采用forceActiveFocus()遍历的焦点树,并设置focustrue为在路上的每个节点,因此Item具有activeFocus底。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM