Unsolved Advice for using proxy models in QML
-
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 aFigure
) which may be populated with data items by the user. The user can load or delete data items in theGlobalDataModel
, and create or destroy figures at runtime.It appears to me like a proxy model intends to solve this problem. Each
Figure
would hold a proxy whose source is theGlobalDataModel
. 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 calledDelegateModel
andDelegateModelGroup
, and the former has afilterOnGroup
property. I have found a number of limitations of that construct, however. For example, it's not possible to dynamically add and remove groups from aDelegateModel
, 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 theGlobalDataModel
thatFigure
should display.Implementing
QAbstractProxyModel
looks 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.
Should a
Figure
only show a single row of yourGlobalDataModel
?
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 ofQObject*
? -
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
GobalDataModel
which the user wants to display in a givenFigure
. There could be any number of figures, each containing an arbitrary subset of the global data items.The
Figure
should access its data items "associatively", not by the physical row index in theGlobalDataModel
.The storage of
GobalDataModel
could, I believe, beQObject*
, or it could also be a POD structure calledDataItem
that is copy-able. One reason I might be reluctant to useQObject*
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 givenFigure
could filter items based on whether theirtags
attribute contains the name of that figure. Would that approach adapt nicely to your project?