简体   繁体   English

QML焦点是如何传播的?

[英]How does QML focus propagate?

I am trying to figure out how to properly set focus in my application. 我试图弄清楚如何在我的应用程序中正确设置焦点。

I have a component MyItem.qml, which I want to change its background when any of its children gets focus. 我有一个组件MyItem.qml,我想在它的任何一个孩子获得焦点时改变它的背景。 I also have a MyDerivedItem.qml that derives from MyItem.qml that also should change the background of the base class if any of its children gets focus. 我还有一个MyDerivedItem.qml派生自MyItem.qml,如果它的任何子节点获得焦点,它也应该改变基类的背景。

If I understood the documentation correctly, if a component gets focus the focus property of all its parents in the hierarchy are set to true (or until a FocusScope component is reached). 如果我正确理解文档,如果组件获得焦点,则层次结构中其所有父项的focus属性将设置为true(或者直到达到FocusScope组件)。

If this is true, then when I press any of the TextFields in MyItem.qml or MyDerivedItem.qml the myItem.focus property should change to true and the background change its color. 如果这是真的,那么当我按下MyItem.qml或MyDerivedItem.qml中的任何TextField时,myItem.focus属性应该更改为true并且背景会更改其颜色。

I have tried to make a small example of what I want to do, but it does not behave as I expect. 我试图做一个我想做的小例子,但它没有像我期望的那样表现。

//main.qml
import QtQuick.Controls 2.0

ApplicationWindow {
    height: 768
    width: 1024
    visible: true

    MyDerivedItem {
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        width: parent.width / 2
    }
    MyDerivedItem {
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        width: parent.width / 2
    }
}

//MyItem.qml
import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle {
    id: myItem

    default property alias data: column.data

    color: focus ? "red" : "green"

    Column {
        id: column

        TextField {
            placeholderText: "Input Text Here"
        }
    }
}

//MyDerivedItem.qml
import QtQuick 2.7
import QtQuick.Controls 2.0

MyItem {
    id: myDerivedItem

    TextField {
        placeholderText: "Derived Input Text Here"
    }

    TextField {
        placeholderText: "Derived Input Text Here"
    }

    TextField {
        placeholderText: "Derived Input Text Here"
    }

    TextField {
        placeholderText: "Derived Input Text Here"
    }

    //...
}

This is somewhat doumented here . 这有点doumented 这里 Accoding to that the propagation is: Qt -> QQuickWindow -> Item-with-focus. 根据传播的结果:Qt - > QQuickWindow - > Item-with-focus。 There is no traversal of the object tree, but the focusing rather happens directly. 没有对象树的遍历,但聚焦反而直接发生。

There is one exception to this rule, that is the FocusScope which acts as the focused Item towards the scene or a FocusScope in a higher hirarchy. 有一个例外,那就是FocusScope充当聚焦Item对场景或FocusScope更高hirarchy。 So basically you can say, in addition to the object-tree, there is a second focus-tree where each node is a FocusScope and all other Items are leaves. 所以基本上你可以说,除了对象树之外,还有第二个焦点树 ,其中每个节点都是FocusScope ,所有其他Items都是叶子。
Each FocusScope -Node might have one child that has focus. 每个FocusScope -Node可能有一个具有焦点的子项。
Children of an Item in the object-tree might be siblings to their object-parents in the focus-tree. 对象树中Item的子节点可能是焦点树中对象父节点的兄弟节点。

The solution to my problem was a minor change. 我的问题的解决方案是一个小的改变。 Adding FocusScope to MyItem.qml as follows: FocusScope添加到MyItem.qml ,如下所示:

//MyItem.qml
import QtQuick 2.7
import QtQuick.Controls 2.0

FocusScope {
    id: focusScope

    default property alias data: column.data

    Rectangle {
        id: myItem

        anchors.fill: parent
        color: focusScope.focus ? "red" : "green"

        Column {
            id: column
            anchors.fill: parent

            TextField {
                placeholderText: "Input Text Here"
            }
        }
    }
}

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

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