簡體   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