Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Qml best way to pass model through loader

Tags:

qt

qml

what is the best way to pass any model (for example a ListModel in main.qml) through loader into an other .qml file?

like image 875
Matthias -_- Avatar asked Jan 28 '26 20:01

Matthias -_-


2 Answers

If the model only needs to be set once, then you can simply do that in the onLoaded signal handler

Loader {
    id: loader

    source: "myelement.qml"

    onLoaded: item.modelProperty = yourModelId;
}

If it needs to be a binding, e.g. if the model is stored in a property which will change during runtime to a different model instance, then a Binding element should work

Binding {
    target: loader.item
    property: "modelProperty"
    value: theModelStoreProperty
}
like image 147
Kevin Krammer Avatar answered Feb 01 '26 15:02

Kevin Krammer


There is really only one way to use a loader. Pass the url of the qml file to the source property.

From http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-loader.html#details

Loader {
       id: myLoader
       source: "MyItem.qml"
    }

It sounds like this is not quite what you are asking though. If you can provide more details on what you are doing we may be able to provide more help.

like image 30
Deadron Avatar answered Feb 01 '26 17:02

Deadron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!