I've some javascript data containing a list of objects which contains other objects and arrays, and append that to a ListModel.
Such as (assume that the data is generated elsewhere and I would like to avoid its structure):
import QtQuick 2.0
Rectangle {
width: 600
height: 300
ListView {
anchors.fill: parent
model: ListModel {
id: listModel
}
delegate: Text {
text: 'key: '+ key + ', subdict["subkey1"]: ' + subdict.subkey1 +
', sublist.count: '+ sublist.count + ', sublist[0]: '+ sublist[0] +
', sublist.get(0): '+ sublist.get(0)
wrapMode: Text.WordWrap
width: parent.width
color: index % 2 == 0 ? "darkgreen" : "brown"
}
Component.onCompleted: {
var values = [
{'key': 'foo1', 'subdict': {'subkey1': 'subfoo1', 'subkey2': 'subbar1'},
'sublist': ['subvalue1', 'subvalue2']},
{'key': 'foo2', 'subdict': {'subkey1': 'subfoo2', 'subkey2': 'subbar2'},
'sublist': ['subvalue3', 'subvalue4', 'subvalue5']},
{'key': 'foo3', 'subdict': {'subkey1': 'subfoo3', 'subkey2': 'subbar4'},
'sublist': []}
]
for (var i in values)
listModel.append(values[i]);
}
}
}
Most parts work well, but I can't find a way to access to the sublist elements. All I can get is the count of them as they're actually a new ListModel, but since I can't use something such as sublist.get(0) it's seems impossible to get the actual content.
Is this a bug, or am I missing something?
var values =[...]) like model directly, and access every item in delegate with "modelData" keyword. Remember that this way any changes in model won't be painted, so you should refresh the whole listview.