QT QML How to chain QAbstractListModel and QSortFilterProxyModel
-
Hi,
I'm working on a project where I get a large JSON as input, this JSON can change at any time. Items can be removed, added and changed.
I want to to be able to access this data in QML. And I want the QML to be able to sort and filter the data. And I want to do it the right way.
I have broken down the input data into a view model in C++, which consists of a number of implementations of QAbstractListModels. As I do not want to expose QLists with elements to QML. And I want to be able to chain those with filters and sorters. So I have implemented a number of QSortFilterProxyModels for that purpose. One of these I want to be able to limit the number of outputs.
So I want something like: List -> Some kind of filtering -> Some kind of sorting -> Limit the output to X items.
But I run in trouble when the input data changes. The output contains empty items. To make it work I have use beginResetModel and endResetModel, instead functions like beginRemoveRows, endRemoveRows etc. Which seems to defeat the purpose of using a QAbstractListModel.
I have created a small sample project: https://gitlab.com/FinitelyFailed/qt-list-limited

-
I haven't ran your example but note that using QSFPM is not a good base for your LimitFilter unless you use some workarounds.
QSFPM doesn't re-run the filters for accepted rows when a new row is added.
If you are limiting to 1 row, and a new row is inserted first, your model will have 2 rows, the previous first row and the new one.
You could fake a dataChanged on the previous row to force a call tofilterAcceptsRow.If you want a 3rd party model doing the work of your JSON to QAIM there's https://github.com/benlau/qsyncable and its QSJsonListModel.
There's also now a new SortFilterProxyModel available in QML, for previous Qt versions there are multiple on Github.
For the LimitFilter I don't know of any from the top of my head but I guess there are some available, or reimplement it yourself but keep the QSFPM limitation in minds. It's was designed to work on the intrinsic data of a row, not it's relative position in the model. -
Hi,
I'm working on a project where I get a large JSON as input, this JSON can change at any time. Items can be removed, added and changed.
I want to to be able to access this data in QML. And I want the QML to be able to sort and filter the data. And I want to do it the right way.
I have broken down the input data into a view model in C++, which consists of a number of implementations of QAbstractListModels. As I do not want to expose QLists with elements to QML. And I want to be able to chain those with filters and sorters. So I have implemented a number of QSortFilterProxyModels for that purpose. One of these I want to be able to limit the number of outputs.
So I want something like: List -> Some kind of filtering -> Some kind of sorting -> Limit the output to X items.
But I run in trouble when the input data changes. The output contains empty items. To make it work I have use beginResetModel and endResetModel, instead functions like beginRemoveRows, endRemoveRows etc. Which seems to defeat the purpose of using a QAbstractListModel.
I have created a small sample project: https://gitlab.com/FinitelyFailed/qt-list-limited
