Qt Qml best way to pass model through loader - qt

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

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
}

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.

Related

QQmlComponent initialization

i have an application which sets properties of Qml from C++, so far i was able to set property of any type. I am able to create QQmlComponent in C++ and set it to property.
But first i need to initialize component with its properties. I can do that with create/beginCreate and set properties. But i cant change created QObject back to QQmlComponent, therefor i cant set it to givent Component type property.
Is there some way how to conver QObject to QQmlComponent ? Or is there any other way to initialize QQmlComponent properties ?
Example, of what i want to achiev in C++, how it looks in qml
import QtQuick 2.3
Loader {
// property of type Component
sourceComponent: Text {
text: "property i want to intialize, this component can be in file"
}
}
Edit: I will clarify what im after. Suppose i have something like JSON given by user.
e.g:
{
"type":"Loader",
"sourceComponent":{
"type":"Text",
"text":"some property of property of type QQmlComponentProperty"
}
}
Then from C++ i create given items, problem is how to create QQmlComponent with user given properties.

how to add dynamically created object into a Container

I am trying to create ListModels dynamically, and insert these ListModels into a Container.
I get a variable number of ListModels, each with variable content, from a backend system. Based on the user's interaction with the GUI, one of those ListModels needs to be loaded into a known ListView. The client wants to avoid Qt/C++, so I am looking to solve this via QML.
How do I get a dynamically created object (in my case, a ListModel) into a Container? This code does not work:
property string strID;
property string strName;
Container {
id: functionListContainer;
}
// create ListModel
var newObject = Qt.createQmlObject("import QtQml.Models 2.14; ListModel { }", mainWindow);
...
// dynamically append elements into the ListModel
newObject.append({ deviceID: strID, deviceName: strName })
...
// add ListModel to Container
functionListContainer.addItem(newObject);
createQmlObject and append seem to work as intended. I get this error when running addItem code above:
"Could not convert argument 0 at"
...
"Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated."
"This will throw a JavaScript TypeError in future releases of Qt!"
Any idea regarding how to get this to work? I know that addItem is expecting an Item, not an Object, but I do not know how to get this to work. I have tried replacing var with property Item newItem and property QtObject newObject [combined with addItem(newObject.item)], and all give (seemingly identical) errors. Is it a simple issue of casting the object into an Item? If so, what is the syntax that needs to be used?
Lastly, assuming I do have a Container with N ListModels, is it possible to refer to the container directly in the ListView? i.e.:
property int idx;
Container {
id: functionListContainer;
}
// add ListModels to container...
// use ListModel inside container
ListView {
...
model: functionListContainer.itemAt(idx);
...
}
I know that ListView is expecting a ListModel or something equivalent. But I am not sure how to connect the ListView with a Container containing ListModels, or if it is even possible.
If I were to summarize my problem, I am trying to have a ListView display different ListModels based on context, and I am trying to do this within a pure-QML framework as much as possible. Both of my questions above related to this. It would be helpful even only to clarify that this is not an option and that it is necessary to use another method like clearing and populating the ListModel (suggestions welcome).
You are trying to add a QObject (since ListModel is inherited from QAbstractListModel, which is in turn inherited from QObject) as a visual item using the addItem function. However, in QML a QObject (or QtObject) is regarded as an "storage element".
What you want to do it add the contentData of the Container:
Container {
id: functionListContainer
}
// create ListModel
var newObject = Qt.createQmlObject("import QtQml.Models 2.14; ListModel { }", mainWindow);
...
// dynamically append elements into the ListModel
newObject.append({ deviceID: strID, deviceName: strName })
...
// add ListModel to Container
functionListContainer.contentData.push(newObject)
The contentData property is the place where all children reside in the Container; QObject and QQuickItem (note that QQuickItem is inherited from QObject).
About referencing the lists, this also becomes easy when using the contentData property:
// create ListModel
var newObject = Qt.createQmlObject("import QtQml.Models 2.14; ListModel { objectName: \"the_list\" }", mainWindow);
...
// add ListModel to Container
functionListContainer.contentData.push(newObject)
console.log(functionListContainer.contentData[0])
will yield:
qml: QQmlListModel(0x55bcb7119720, "the_list")
Note that this is almost the same as using a Javascript array, the documentation of QQmlListProperty (which is what contentData is) states it is transparent.

How can I populate a TableView dynamically?

If I want to build out a TableView dynamically by loading data via AJAX call rather than by specifying list elements manually (like in the code below), how can that be done? I haven't found any examples anywhere yet.
ListModel {
id: dataModel
ListElement { title:"Image title"; credit:"some author"; source:"http:/..." }
ListElement { title:"Another title"; credit:"some author"; source:"http:/..." }
}
You need to fetch the data and then append it to your model.
The docs on ListModel are pretty straight forward and clear on how to add data to a model dynamically.
As to making the web request, here's an example on how to fetch data in QML using XMLHttpRequest.

How to check if qml component object (that is created dynamically) has been created?

I have a component that should be created dynamically.
Component {
id: myComponent
Rectangle {
id: letItBeRect
}
}
I want to create it dynamically (when some button clicked) in a function like this:
function loadComponent() {
myComponent.createObject(root); //root is some root component, doesn't matter
}
I have state for root component that depends on some letItBeRect property:
state: letItBeRect.visible ? "visible" : "hidden"
So the question is how do I check if letItBeRect has been created, so I could assign a proper value to "state" property of root component?
I get "ReferenceError: letItBeRect is not defined" so far, which is expected from this code snippet.
P.S. This is not the real code I have, because I don't want to put commercial code here. Thank you

Dead QML elements receiving signals?

I have code similar to the following:
...
id: myComponent
signal updateState()
property variant modelList: []
Repeater {
model: modelList
MyButton {
...
Connection {
target: myComponent
onUpdateState: {
...
}
}
}
}
I assign a value to modelList and then issue myComponent.updateState() to update the MyButton components in the repeater. At this point I get a lot of warnings about non existent properties
It seems like the signal gets passed to the MyButton(s) that doesn't exist anymore (since the repeater will rerun when I change modelList).
Is there a way of avoiding this or should I simply ignore the warnings?
I had a similar problem when destroying QML components connected to C++ signals. The problem was solved by adding a handler for disconnecting the signals when the components were destroyed. In the dynamically generated components you could try manually connecting the signals, in order to manually disconnect them on destruction. In my case the code looks something like this:
MyComponent {
Component.onCompleted: signal.connect(callback_function)
Component.onDestruction: signal.disconnect(callback_function)
function callback_function() {
// process signal
}
}
It might be, there is a better solution not needing manually connecting and disconnecting the signals, but this worked for me. If you add a console.log("Destroying...") to the onDestruction handler you can check whether or not the components are disconnecting the signal, thus actually being destroyed.

Resources