How to create grouped / nested properties?

I am trying to execute nested properties such as "font.family" or "anchors.fill", but I cannot initialize them in the usual way, because it prints "Unable to assign a nonexistent property". Instead, I am forced to use the Component.onCompleted method. What's wrong?

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 } } } } 
+6
source share
3 answers

Try replacing your nested QtObject QML file. For example, I replaced it with BackgroundTheme.qml . Thus, a property (which can be correctly called a "grouped property") works correctly, in a binding and without errors.

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 {} } 
+12
source

To add to the wrong part of your question.

Your approach does not work because left expressions are statically typed in QML.

I had the same issue and found this Matthew Vogt comment on the QT error message:

This behavior is currently correct.

QML is statically typed for expressions on the left, so dynamic properties that are not in the static property type cannot be resolved during initialization.

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

The previous comment gives a good example:

This error is not specifically related to the use of an alias - it is the result of searching for properties at compile time in different modules. 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 search for 'fg' succeeded in initializing the 'bar' inside Base.qml, but did not execute in main.qml.

+1
source

This may not be the right way. But it works

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

Now using background.enabled: "#1B2E0A" in main.qml is working.

0
source

Source: https://habr.com/ru/post/981601/


All Articles