Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Advice for using proxy models in QML
Dr. Z last edited by
I am looking for advice on how the following scenario could be best implemented in QML and C++.
I am writing a data visualization application, comprising an application-wide store of data items (say the
GlobalDataModel) and a set of views (let's all each a
Figure) which may be populated with data items by the user. The user can load or delete data items in the
GlobalDataModel, and create or destroy figures at runtime.
It appears to me like a proxy model intends to solve this problem. Each
Figurewould hold a proxy whose source is the
GlobalDataModel. The proxy should act as a filter on the source model, where the controller adds specific source indexes to the proxy. I also noticed there is a QML construct called
DelegateModelGroup, and the former has a
filterOnGroupproperty. I have found a number of limitations of that construct, however. For example, it's not possible to dynamically add and remove groups from a
DelegateModel, unless I am missing something.
Best I can tell, what I need is another model, say a
FiguresModel, which contains one entry for each figure - each containing persistent model indexes of the data elements from the
QAbstractProxyModellooks somewhat intimidating, and as I'm not a Qt expert yet I'm concerned there may be pitfalls.
Has anybody implemented something logically similar to this? Am I making it too complicated, or expecting too much from QML? Any feedback is greatly appreciated.
I've implemented this : https://github.com/oKcerG/SortFilterProxyModel
It lets you create a SortFilterProxyModel in QML and allows you to conveniently add multiple filters and sorters for the proxy model.
DelegateModel looks like a nightmare to use and seemed like a hack more than a proper solution from my point of view.
I've never used it and don't really know a legit usecase for it, maybe reordering models only on the UI side?
I've not really understood all your needs so I'll ask some questions for clarification.
Figureonly show a single row of your
When you say "each containing persistent model indexes of the data elements from the GlobalDataModel", what do you mean by persistent model indexes? If rows are inserted before the ones you selected, should it follow the old rows or the rows that took their places?
By what storage will your GobalDataModel be backed? A container of
Dr. Z last edited by
Hi GrecKo, thanks very much for your reply. I was aware of your project, and I think it might be right.
What I need is for the "filter" to pick out those items in the
GobalDataModelwhich the user wants to display in a given
Figure. There could be any number of figures, each containing an arbitrary subset of the global data items.
Figureshould access its data items "associatively", not by the physical row index in the
The storage of
GobalDataModelcould, I believe, be
QObject*, or it could also be a POD structure called
DataItemthat is copy-able. One reason I might be reluctant to use
QObject*is that I don't fully understand the Qt/QML ownership policy. Let's assume for now the data item is POD.
I wonder if it might make sense to add a list of "tags" to each data item, a la gmail (one tag per
Figure). Then the proxy model for a given
Figurecould filter items based on whether their
tagsattribute contains the name of that figure. Would that approach adapt nicely to your project?