Important: Please read the Qt Code of Conduct -

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 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 Figure would 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 DelegateModel and DelegateModelGroup, and the former has a filterOnGroup 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 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 GlobalDataModel that Figure 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.

  • Qt Champions 2018

    I've implemented this :
    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 your GlobalDataModel ?
    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 QObject*?

  • 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 given Figure. 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 the GlobalDataModel.

    The storage of GobalDataModel could, I believe, be QObject*, or it could also be a POD structure called DataItem that 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 Figure could filter items based on whether their tags attribute contains the name of that figure. Would that approach adapt nicely to your project?

Log in to reply