繁体   English   中英

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

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

我是 QML 的新手,我对 FocusScope 类型感到困惑。 我的理解是,当多个项目请求焦点时,它用于控制部分应用程序的焦点。

文档说:

从概念上讲,焦点范围非常简单。

在每个焦点范围内,一个元素可能会将 Item::focus 设置为 true。 如果多个 Item 设置了焦点属性,则设置焦点的最后一个元素将具有焦点,而其他元素未设置,类似于没有焦点范围的情况。

那么,当程序运行时,为什么下面代码中的第一个文本字段聚焦而不是最后一个?

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
            }
        }
    }
}

任何帮助是极大的赞赏!

设置属性的顺序未定义

作为一般规则,用户不应依赖绑定的评估顺序。

您可以使用以下代码查看它们实际设置的顺序:

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)
            }
        }
    }
}

这是输出:

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

您一次只能有一个具有活动焦点的项目,因此为多个同级设置焦点为 true 没有多大意义。

如果你想让第一个TextField专注于启动,我会这样做:

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

暂无
暂无

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

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