QML - signals for properties of nested objets
-
Re: QML - signals for nested properties
In fact, I would like to read a JSON file and store it into a variable.
Ideally, components will read/write/connect to the nested properties.I fail to connect to signals of nested properties.
I tried several approaches:property var test: { "a": "a value", "nested": { "inner": 5 } } // property alias aliasTestA: test.a // does not compile: Invalid alias reference. Unable to find id "test" property var bindingTestA: test.a property var bindingTestNestedInner: test.nested.inner Connections { target: item function onTestChanged() { console.debug("onTestChanged", test) } } Connections { target: test function onAChanged() { console.debug("test.onFooChanged", test.a) } } Connections { target: test.nested function onInnerChanged() { console.debug("test.nested.onInnerChanged", test.nested.inner) } } // onAliasTestAChanged: console.debug("onAliasTestAChanged", test.a) onBindingTestAChanged: console.debug("onBindingTestAChanged", test.a) onBindingTestNestedInnerChanged: console.debug("onBindingTestNestedInnerChanged", test.nested.inner) Component.onCompleted: { test.nested.inner = 6; test.nested = { "inner": 7} test.a = "baz" test = { "a": "arg", "nested": { "inner": 8 } } }I got:
QML Connections: Detected function "onInnerChanged" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name. onBindingTestAChanged a value onBindingTestNestedInnerChanged 5 onBindingTestAChanged arg onBindingTestNestedInnerChanged 8 onTestChanged [object Object]Adding binding for each property is not really simple and time effective.
It also miss a change: inner = 7.Is there any tips to connect to signals in an effective way?
-
@jeremy_k Ok right!
Is there any way to convert this json object into a QObject with dynamic properties and signals?
-
I think the only way for you is to use a QtObject and declare properties for it:
property QtObject test: QtObject { property string a; etc... } -
I think the only way for you is to use a QtObject and declare properties for it:
property QtObject test: QtObject { property string a; etc... }@Marko-Stanke said in QML - signals for properties of nested objets:
I think the only way for you is to use a QtObject and declare properties for it:
property QtObject test: QtObject { property string a; etc... }Yes, QML's QtObject is a QObject. Item, Rectangle, and most instantiable types know to the QML engine would also work to host properties. If you don't need any of their extra capabilities, then QtObject is the leanest QML language option.