Given a ListModel with multiple layers of stored information (arrays of elements stored within elements), is there a way to store the model and recall it later?
I've tried storing ListModel as a JSON string, but it doesn't keep track of child objects. For example, in the following snippet, the output tells me there is a "kids" object, but has no knowledge of "kid1" nor "kid2". Same goes for the "store" object, but no knowledge of "duck1" nor "duck2".
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
ListModel {
id: listModel
ListElement {
name: "Parent"
kids: [
ListElement {kid: "kid1"},
ListElement {kid: "kid2"}
]
}
ListElement {
name: "store"
ducks: [
ListElement {duck: "duck1"},
ListElement {duck: "duck2"}
]
}
Component.onCompleted: {
var datamodel = []
for (var i = 0; i < this.count; ++i)
datamodel.push(this.get(i))
console.log(JSON.stringify(datamodel))
}
}
}
Here is the output, which fails to show any information about the child objects. I would expect there to be "kid1" and "kid2" under the "Parent" object.
[
{
"kids": {
"objectName": "",
"count": 2,
"dynamicRoles": false
},
"name": "Parent"
},
{
"name": "store",
"ducks": {
"objectName": "",
"count": 2,
"dynamicRoles": false
}
}
]
Edit: I would expect the output to be more like this:
[
{
"name": "Parent",
"kids": [
{
"kid": "kid1"
},
{
"kid": "kid2"
}
]
},
{
"name": "store",
"ducks": [
{
"duck": "duck1"
},
{
"duck": "duck2"
}
]
}
]
console.log(JSON.stringify(listModel.get(0).kids))then I get{"objectName":"","count":2,"dynamicRoles":false}which shows that the 'kids' array is being read as an object... most unfortunate. Tryingconsole.log(JSON.stringify(listModel.get(0).kids.get(0)))is better, it at least gives{"kid":"kid1"}but creating a recursive loop for the entire tree... is this good practice if the tree is 10-layers deep? That's an exponentially growing for loop if it's 10 layers. - Tyler M