Reputation: 305
I do have a bunch of xml files named "config_1.xml", "config_2.xml", "config_3.xml", and so on.
Basically, I need to loop from first one to last one, appending in the SAME XmlListModel all the "id" that I find at the query context/info/parameters in each xml file.
So, once the reading is completed, I will have the whole list of id(s).
I tried with:
property int myIterator: 1
Repeater {
model: 10 //10 is just an example. I have this number in my code by other functions.
XmlListModel {
id: myModel
source: "qrc:/ConfigFiles/config_" + myIterator + ".xml" // <---- THIS IS THE PROPERTY I NEED TO PARAMETRIZE!!!
query: "context/info/parameters"
XmlRole { name: "myId"; query: "id/number()" }
onCountChanged: { ++myIterator; }
}
}
onMyIteratorChanged: {
if (myIterator> 10)
for(var i = 0; i<myModel.count; i++) // myModel.count should be equal to 10!!!
console.log("id: " + myModel.get(i).myId);
}
However, with this code the application crashes. How can I fix it?
I also considered the idea of transferring all the information stored in myModel at each iteration into a ListModel (in which the appen() is easy by documentation). But I still don't know how to call XmlListModel n times...
Thank you!! :-)
Upvotes: 1
Views: 693
Reputation: 7150
To create multiple XmlListModel
based on a model, you can't use a Repeater
since XmlListModel
is not an Item
. You have to use Instantiator
for plain QtObject
.
Similarly to a Repeater
you can access the data of a model in the delegate with modelData
(or index
, it's the same here since we are using an integer as a model).
If you want to iterate over all your model you can do so with count
and objectAt()
:
Instantiator {
id: instantiator
model: 10
delegate: XmlListModel {
source: "qrc:/ConfigFiles/config_" + (modelData + 1) + ".xml"
query: "context/info/parameters"
XmlRole { name: "myId"; query: "id/number()" }
}
Component.onCompleted: {
for (var i = 0; i < instantiator.count; i++) {
var xmlModel = instantiator.objectAt(i);
for (var j = 0; i < xmlModel.count; j++)
console.log("id: " + xmlModel.get(j).myId);
}
}
Upvotes: 2