Is it possible to dynamically assign a component?
-
@Bob64 yes I have looked at those. I can go that route if necessary -- I just wanted to see if I was overlooking something far simpler (which I often do).
I tried this:
Loader { id: drawerLoader active: drawer.opened } Component.onCompleted: { drawerLoader.setSource(tile.drawerFile, {"titleText": tile.titleText}) }
Curiously, this loaded the file, but my argument didn't come through...
-
This is not possible. Better you can look at createQmlObject(..). You can build string dynamically & use for creation of objects.
-
Let me ask the question differently, since I still have the feeling I'm overcomplicating this.
I want a QML object that I can reuse. The object will contain a Drawer, the contents of which will be different for each instance of the object. I also need to pass arguments into the object. How best to do this?
Thanks...
-
Let me ask the question differently, since I still have the feeling I'm overcomplicating this.
I want a QML object that I can reuse. The object will contain a Drawer, the contents of which will be different for each instance of the object. I also need to pass arguments into the object. How best to do this?
Thanks...
@mzimmers said in Is it possible to dynamically assign a component?:
I want a QML object that I can reuse. The object will contain a Drawer, the contents of which will be different for each instance of the object. I also need to pass arguments into the object. How best to do this?
Declarative-ly.
Take a look at the 'model' property in Repeater, for instance.
If this is specifically about a Loader, you can use the 'item' property and the onLoaded callback.
Loader { id: foo onLoaded: { item.model = something; } }
-
@mzimmers said in Is it possible to dynamically assign a component?:
I want a QML object that I can reuse. The object will contain a Drawer, the contents of which will be different for each instance of the object. I also need to pass arguments into the object. How best to do this?
Declarative-ly.
Take a look at the 'model' property in Repeater, for instance.
If this is specifically about a Loader, you can use the 'item' property and the onLoaded callback.
Loader { id: foo onLoaded: { item.model = something; } }
@TomZ thanks for the suggestion. Are you sure your code example is correct? I tried this:
Loader { id: drawerLoader active: drawer.opened source: tile.drawerFile onLoaded: { item.model = 10; } }
And got an error: Error: Cannot assign to non-existent property "model"
-
Loader takes a
source
property which points to your re-usable QML component. (or sourceComponent, check the docs).The actual reusable QML component in my example is not listed. It would be the one that has a
property QtObject model: null
or similar, which it uses internally to 'do stuff'.My example is thus purely meant to illustrate the way for you to assign your data to the re-usable component you would write yourself.
-
Loader takes a
source
property which points to your re-usable QML component. (or sourceComponent, check the docs).The actual reusable QML component in my example is not listed. It would be the one that has a
property QtObject model: null
or similar, which it uses internally to 'do stuff'.My example is thus purely meant to illustrate the way for you to assign your data to the re-usable component you would write yourself.
Hey everybody -
Thanks for all the good suggestions. I fill file them away for future use. For my current issue, here's how I solved it:
Rectangle { id: tile property url drawerFile: "" // supplied by caller property string titleText: "" // supplied by caller property string statusText: "" // supplied by caller Drawer { id: drawer Loader { id: drawerLoader anchors.fill: parent active: drawer.opened source: tile.drawerFile onLoaded: { item.titleText = tile.titleText item.statusText = tile.statusText } } } }
This is what I was looking for: both the item itself (tile.drawerfile) and arguments to the item (item.titleText and item.statusText) can be expressed with variables (properties).
Thanks again...
-
Hey everybody -
Thanks for all the good suggestions. I fill file them away for future use. For my current issue, here's how I solved it:
Rectangle { id: tile property url drawerFile: "" // supplied by caller property string titleText: "" // supplied by caller property string statusText: "" // supplied by caller Drawer { id: drawer Loader { id: drawerLoader anchors.fill: parent active: drawer.opened source: tile.drawerFile onLoaded: { item.titleText = tile.titleText item.statusText = tile.statusText } } } }
This is what I was looking for: both the item itself (tile.drawerfile) and arguments to the item (item.titleText and item.statusText) can be expressed with variables (properties).
Thanks again...
I imagine what you want is something of this sort:
Item { id: tile property alias delegate: loader.source property alias opened: loader.active property string titleText: "" property string statusText: "" Loader { id: loader anchors.fill: parent active: false onLoaded: { item.titleText = Qt.binding(function() { return tile.titleText }) item.statusText = Qt.binding(function() { return tile.statusText }) } } }
You do want the property to update if/when it's changed, and also not to define properties needlessly - just alias what you can.
PS.
Or even better would be to aliassourceComponent
of theLoader
, not the url proper. -
I imagine what you want is something of this sort:
Item { id: tile property alias delegate: loader.source property alias opened: loader.active property string titleText: "" property string statusText: "" Loader { id: loader anchors.fill: parent active: false onLoaded: { item.titleText = Qt.binding(function() { return tile.titleText }) item.statusText = Qt.binding(function() { return tile.statusText }) } } }
You do want the property to update if/when it's changed, and also not to define properties needlessly - just alias what you can.
PS.
Or even better would be to aliassourceComponent
of theLoader
, not the url proper.@kshegunov thanks for the suggestions. I'm curious why you think it would be better to use sourceComponent instead of source.
-
@kshegunov thanks for the suggestions. I'm curious why you think it would be better to use sourceComponent instead of source.
@mzimmers said in Is it possible to dynamically assign a component?:
@kshegunov thanks for the suggestions. I'm curious why you think it would be better to use sourceComponent instead of source.
It's much more flexible. I can define the component in the source of the user file (the QML that uses the above), or I can instantiate it from a QML file by name, or - god forbid ;) - expose it from C++ ...
e.g.
Component { id: someComponent ComboBox {} } MyTile { delegate: someComponent opened: true titleText: "whatever" }