Jongling Qt models 2, Composition Gem

We are back to models again :). In Qt you may find a lot of flexibilities for Model-View programming. Especially due to proxies to do filtering, sorting or even rearranging data (see Jongling Qt models) to be organized in any way that is good for you. You can have one data source model which you then split in “flows” of data with chains of proxy models which have ends at views. For example friend of mine found that it is very effective to have one single model for all data loaded which then goes in 3-4 chains of about 15 proxy models in totals. Why so much? – Every proxy is as much simple operation as possible, for example: filter, ungroup, transpose, etc. Each simple operation much easier to implement, debug and prove effectiveness. But by joining them in chain you can make any complicated operations. You have to track complexity that you get at the end of chain – i.e. sorting proxy will add O(n*log(n)), but if you make filtering before sorting, then it goes faster.

But you may encounter one area which is totally uncovered – there is no way of joining of models into one composition proxy model. So I would like to present some “Gem” for Qt model-view programmers: RowsJoinerProxy class. It does very simple thing: join all top level rows from first source model, second, etc. It does not affect children, so all hierarchies of source models are kept. For root columns it uses qMax(columCount() of roots of source models). Rows inserts, rows removals, data changes of source models are recognized and mapped. Column operations are not supported at the moment, so I may update it later. Inserting/Removing source model from proxy makes model reset, though it can be improved later to do just rows removal.


This entry was posted in Models, Qt, Research. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Victor Shcherbinin
    Posted June 12, 2014 at 11:35 | Permalink

    Is it correct to access protected method createIndex() of the source model using reinterpret_cast trick? What if some of the source models use internalIndex and not internalPointer in their model indexes?

  2. yshurik
    Posted June 12, 2014 at 11:42 | Permalink

    internalId and internalPointer are same. It is just “void *p” in private of QModelIndex class.

  3. Falco Girgis
    Posted October 27, 2014 at 11:17 | Permalink

    Beautiful, man. Just beautiful. I was scouring the internet hoping to find a solution as elegant as this… I’m shocked this isn’t offered as a standard Qt class.nnGoing to be using this to represent Lua tables uniformly as aggregates data models within our Toolkit for Elysian Shadows. Thanks, you’re now in the credits.

  4. Yash pal
    Posted January 3, 2015 at 14:59 | Permalink

    Thank you :)

  5. chabo
    Posted October 23, 2016 at 09:58 | Permalink

    Working for QDirModels but doesn’t work for newer QFileSystemModels. Seems it has something to do with canFetchMore, fetchMore functionality. Does anyone update this gem to support this feature? It would be really helpful.

One Trackback

  1. By Jongling Qt models on September 29, 2011 at 22:59

    […] Read also Jongling Qt models 2, Composition Gem This entry was posted in Blog, Models, Qt, Research. Bookmark the permalink. Post a comment or […]

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>