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.
5 Comments
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?
internalId and internalPointer are same. It is just “void *p” in private of QModelIndex class.
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. http://www.elysianshadows.com.
Thank you :)
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
[…] Read also Jongling Qt models 2, Composition Gem This entry was posted in Blog, Models, Qt, Research. Bookmark the permalink. Post a comment or […]