简体   繁体   English

多个请求焦点的子项如何在 FocusScope 类型中获得焦点?

[英]How do multiple child items requesting focus get focus within a FocusScope type?

I'm new to QML and I'm confused by the FocusScope type.我是 QML 的新手,我对 FocusScope 类型感到困惑。 My understanding is that it's used to control the focus of part an application when multiple items are requesting focus.我的理解是,当多个项目请求焦点时,它用于控制部分应用程序的焦点。

The documentation says:文档说:

Conceptually focus scopes are quite simple.从概念上讲,焦点范围非常简单。

Within each focus scope one element may have Item::focus set to true.在每个焦点范围内,一个元素可能会将 Item::focus 设置为 true。 If more than one Item has the focus property set, the last element to set the focus will have the focus and the others are unset, similar to when there are no focus scopes.如果多个 Item 设置了焦点属性,则设置焦点的最后一个元素将具有焦点,而其他元素未设置,类似于没有焦点范围的情况。

So, when the program is run, why is the first textfield in the following code focused and not the last?那么,当程序运行时,为什么下面代码中的第一个文本字段聚焦而不是最后一个?

import QtQuick 2.0
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.15

ApplicationWindow{
    visible: true
    width: 200
    height: 200

    FocusScope{

        Column{
            Text{text: "first focus scope"}
            TextField {
                width: 100; height: 25; focus: true
                text: focus
            }
            TextField{
                width: 100; height: 25; focus: true
                text: focus
            }

            Text{text: "second focus scope"}
            TextField {
                width: 100; height: 25; focus: true
                text: focus
            }
            TextField{
                width: 100; height: 25; focus: true
                text: focus
            }
        }
    }
}

Any help is greatly appreciated!任何帮助是极大的赞赏!

The order in which properties are set is undefined :设置属性的顺序未定义

As a general rule, users should not rely on the evaluation order of bindings.作为一般规则,用户不应依赖绑定的评估顺序。

You can see which order they're actually set in with the following code:您可以使用以下代码查看它们实际设置的顺序:

import QtQuick 2.0
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.15

ApplicationWindow {
    width: 200
    height: 200
    visible: true

    FocusScope {

        Column {
            Text {
                text: "first focus scope"
            }
            TextField {
                objectName: "textField1"
                width: 100
                height: 25
                text: focus
                focus: {
                    print(objectName, "setting focus to true")
                    true
                }
                onFocusChanged: print(objectName, "focus changed to", focus)
            }
            TextField {
                objectName: "textField2"
                width: 100
                height: 25
                text: focus
                focus: {
                    print(objectName, "setting focus to true")
                    true
                }
                onFocusChanged: print(objectName, "focus changed to", focus)
            }

            Text {
                text: "second focus scope"
            }
            TextField {
                objectName: "textField3"
                width: 100
                height: 25
                text: focus
                focus: {
                    print(objectName, "setting focus to true")
                    true
                }
                onFocusChanged: print(objectName, "focus changed to", focus)
            }
            TextField {
                objectName: "textField4"
                width: 100
                height: 25
                text: focus
                focus: {
                    print(objectName, "setting focus to true")
                    true
                }
                onFocusChanged: print(objectName, "focus changed to", focus)
            }
        }
    }
}

This is the output:这是输出:

qml: textField4 setting focus to true
qml: textField4 focus changed to true
qml: textField3 setting focus to true
qml: textField4 focus changed to false
qml: textField3 focus changed to true
qml: textField2 setting focus to true
qml: textField3 focus changed to false
qml: textField2 focus changed to true
qml: textField1 setting focus to true
qml: textField2 focus changed to false
qml: textField1 focus changed to true

You can only have one item with active focus at a time, so setting focus to true for more than one sibling doesn't make much sense.您一次只能有一个具有活动焦点的项目,因此为多个同级设置焦点为 true 没有多大意义。

If you want to eg have the first TextField have focus on startup, I'd just do this:如果你想让第一个TextField专注于启动,我会这样做:

ApplicationWindow {
    // ...
    Component.onCompleted: textField1.forceActiveFocus()

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

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