ListModel recursively modifies JS arrays of appended objects - bug or feature?
-
The code below shows that JS objects appended to a ListModel
are modified such that ALL arrays, that are defined in the object
are converted to ListModels itself. So it defines somehow a tree of ListModels.
Is this a feature or a bug? If a feature: what is the logic behind this?Thanks for feedback!
Frank
@
import Qt 4.7Rectangle {
ListModel {
id: model
}
Component.onCompleted: {
var itemIn = {
foo: [1,2,3]
};
model.append(itemIn);
var itemOut = model.get(0);
console.log("itemIn.foo:", itemIn.foo, "itemOut.foo:", itemOut.foo);
}
}
@-> output: itemIn.foo: 1,2,3 itemOut.foo: QDeclarativeListModel(0x1022a7830)
-
This is done by design. Any list item added to a ListModel is taken to be a "sub-list model".
This allows any list within a list model to also be used as a model, which is useful for creating views for such sub-models. For example:
@
import Qt 4.7
Rectangle {
width: 200; height: 200ListModel { id: model } ListView { model: model anchors.fill: parent delegate: Column { Repeater { model: outer Text { text: inner } } } } Component.onCompleted: { model.append({'outer': [ {'inner': 'aaa'}, {'inner': 'bbb'}]}) model.append({'outer': [ {'inner': 'ccc'}, {'inner': 'ddd'}]}) }
}
@The inner view can use 'outer' as a list model in order to render the 'inner' values.
However, the fact that a list like "[1,2,3]" can be added as an item is a bug. It should only allow lists of objects to be added. I've added a link to this post in the relevant bug report (http://bugreports.qt.nokia.com/browse/QTBUG-13640).