Create sub a ListModel
-
Hi,
I need to create a sub list of a ListModel, For example I have huge list of things and I want to get them by 10, like I could want a sub list which start width the 8th element and take 12 of them.
What's the best way of doing that ?
Thanks a lot.
-
I guess it depends on what you really need.
My first hunch would be to subclass a QSortFilterProxyModel, and expose that with some relevant properties to QML to provide the data you need.
-
Thanks for answering. I need that : @
ListModel{
id:lmodel
//[...]
}SubListModel{
id:lmodel1
source:lmodel
range.start:0
range.lenght:10
}ListView{
model:lmodel1
//[...]
}@Is it possible with your suggestion ?
-
Not with just QML, no. My suggestion would take C++ to realize.
-
Ok, I get that, so subclass a QSortFilterProxyModel would solve my problem ?
-
It could, if your data doesn't come from within QML. If you can get your data from the C++ side, you can use this approach. The problem is that you can not use a QSortFilterProxyModel on a model that comes from QML, but only on a QAbstractItemModel-derived model.
-
Arf... My data came from a QML ListModel since I work only with QML and some few C++plugins.
-
Well, maybe you can do it by adding a javascript-function that will loop through your ListModel (lmodel) and add the desired item to other ListModel (lmodel1).
-
That's what I think of, but I'm not quite sure I can do that : @list.append(list2.get(i))@
-
Is this what you're looking for?
@
import QtQuick 1.0Rectangle {
id: mainwidth: 400 height: 600 ListModel { id: myModel ListElement { name: "element 0" } ListElement { name: "element 1" } ListElement { name: "element 2" } ListElement { name: "element 3" } } ListModel { id: subList } ListView { id: listView anchors.fill: parent spacing: 8 model: subList delegate: Text { text: name; width: listView.width } } Text { text: "Update list" anchors.right: parent.right; anchors.bottom: parent.bottom; anchors.margins: 5 MouseArea { anchors.fill: parent onClicked: updateSubList(1, 2) } } function updateSubList(begin, end) { subList.clear() end = Math.min(end, myModel.count-1) for (var i = begin; i <= end ; i++) { subList.append(myModel.get(i)) } }
}
@ -
Yeah that should do the tricks, thanks a lot.....
And what happen if the source model change ? Is there a signal emit somewhere, or did i need to build one ? -
I actually tried connecting to a 'onCountChanged' signal on the ListModel but w/o success. Don't know if that is a bug or feature :)