Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Advice for using proxy models in QML

Advice for using proxy models in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 6.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dr. Z
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      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 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*?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dr. Z
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved