Solved Component and Loader where there's a property alias between the Component Item and the Loader?
-
Hi!
Let's say I have a
Component
that contains aRadialGradient
.Component { id: radGrad RadialGradient { //stuff here } }
Now I use a Loader to create it and bind it to a
Rectangle
(which is essentially an opacity mask)Rectangle { id: wrecked x:0; y:0; width: 256; height: 256 //LOL! Coder numbers! radius: 32 } Loader { sourceComponent: radGrad anchors.fill: parent // Loader already has a source property - RadialGradient source property is "hidden" source: wrecked // BANG! }
As you can see both the
Loader
and theRadialGradient
have asource
property and theLoader
property, naturally, wins out. So how do I set the source property of the inner item? I can't add a property alias to the inlineComponent
becauseComponent objects cannot declare new properties.
Currently the only decent work around I have is this...
Loader ... onLoaded: { item.source = wrecked }
But this doesn't "feel" good (should be a bind anyway). I can't write
item.source
inside theLoader
either.What's a better solution?
Yours,
Matthew -
Hi @kindid and welcome
you can define properties inside your loader component, that than can be accessed by the loaded item/component
something like this:Loader { sourceComponent: radGrad anchors.fill: parent // Loader already has a source property - RadialGradient source property is "hidden" property var wreckedSource: wrecked } Component { id: radGrad RadialGradient { source: wreckedSource } }
-
Another solution is using
Binding
:Loader { id: loader sourceComponent: radGrad anchors.fill: parent Binding { target: loader.item property: "source" value: wrecked } }
-
Both work nicely. I will probably go with
Binding
as it means the interface to theItem
contained within theComponent
maintains the same property set as is documented. But the use of aLoader
defined property is a very interesting use of the dynamic search space from the perspective of theComponent
encapsulatedItem
. I may have other uses for that - not sure yet!Thank you very much!