How to create grouped/nested properties?

后端 未结 3 1864
失恋的感觉
失恋的感觉 2021-01-01 00:57

I am trying to do nested properties like \'font.family\' or \'anchors.fill\', but I cannot initialize them in normal way because it prints \'Cannot assign to non-existent pr

3条回答
  •  我在风中等你
    2021-01-01 01:13

    To add to the What's wrong part of your question.

    Your approach does not work, because left-hand-side expressions are statically typed in QML.

    I had the same problem and found this comment by Matthew Vogt to a QT bug report:

    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.

    The previous comment gives a good example:

    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.

提交回复
热议问题