如何创建 grouped/nested 属性?

How to create grouped/nested properties?

我正在尝试像 'font.family' 或 'anchors.fill' 这样的嵌套属性,但我无法以正常方式初始化它们,因为它会打印 'Cannot assign to non-existent property'。相反,我被迫使用 Component.onCompleted 方法。怎么了?

MyButtonStyling.qml:

import QtQml 2.1

QtObject
{
    property QtObject background: QtObject
    {
        property color pressed: "#CCCCCC"
        property color enabled: "#666666"
        property color disabled: "#555555"
    }
}

main.qml:

import QtQuick 2.0

Item
{
    width: 400
    height: 300
    MyButton
    {
        text: "TEST"
        styling: MyButtonStyling
        {
            //background.enabled: "#1B2E0A" //Cannot assign to non-existent property "enabled"
            Component.onCompleted:
            {
                background.enabled = "#1B2E0A" //Works
            }
        }
    }
}

MyButton.qml:

import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.0

Button
{
    property QtObject styling: MyButtonStyling {}

    implicitWidth: 80
    implicitHeight: 80

    style: ButtonStyle
    {
        background: Item
        {
            Rectangle
            {
                anchors.fill: parent
                color: control.pressed ? styling.background.pressed : control.enabled ? styling.background.enabled : styling.background.disabled
            }
        }
    }
}

尝试用 QML 文件替换嵌套的 QtObject。例如,我将其替换为BackgroundTheme.qml。这样,属性(可以正确地称为 "grouped property")在绑定中正确工作,没有任何错误。

BackgroundTheme.qml

import QtQuick 2.0

QtObject {
  property color pressed: "#CCCCCC"
  property color enabled: "#666666"
  property color disabled: "#555555"
}

MyButtonStyling.qml

import QtQuick 2.0

QtObject {       
    property BackgroundTheme background: BackgroundTheme {}
}

添加问题的问题 部分。

您的方法不起作用,因为左侧表达式在 QML 中是静态类型

我遇到了同样的问题并发现 this comment by Matthew Vogt 到 QT 错误报告:

This behavior is currently correct.

QML is statically typed for left-hand-side expressions, so dynamic properties not present in the static type of a property cannot be resolved in initialization.

The workaround for this issue is to move the declaration of the type to be initialized to its own file, so that it can be declared as a property with a correct static type.

previous comment给出了一个很好的例子:

This bug is not specifically related to the use of the alias - it results from property lookup during compilation in different units. A simple example:

Base.qml

import QtQuick 2.0

Item {
    property QtObject foo: Image {
        property color fg: "red"
    }

    property color bar: foo.fg
}

main.qml

import QtQuick 2.0

Base {
    foo.fg: "green"
}

Here, 'foo' has the static type 'QtObject' and the dynamic type 'QQuickImage_QML_1'. The lookup of 'fg' is successful in the initialization of 'bar' inside Base.qml, but fails in main.qml.

这可能不是正确的方法。但这有效

MyButtonStyling.qml

import QtQml 2.1

QtObject
{
    property alias background: _obj_background

    QtObject 
    {
        id: _obj_background
        property color pressed: "#CCCCCC"
        property color enabled: "#666666"
        property color disabled: "#555555"
    }
}

现在可以在 main.qml 中使用 background.enabled: "#1B2E0A"