PDA

View Full Version : Dynamic Grouping of QTableView Rows



stefanadelbert
3rd December 2010, 00:21
I would like to be able to group the rows in a QTableView by a certain colum. For example, if I had a table showing:

Item Price
Hat 10
Belt 5
Suitcase 10
Umbrella 5

I would like to be able to group it by price, and the result would be something like

Item
+ Price 10 (2)
+ Price 5 (2)

And with the nodes expanded

Item
- Price 10 (2)
Hat
Suitcase
- Price 5 (2)
Umbrella
Belt

This would need to be implemented with a QTreeView, of course. Has anyone done this before? Are there any open source projects that does this that I could have a look at?

Any help welcome.

Thanks

franz
3rd December 2010, 21:34
There's several possibilities. You can work with the table model and implement a sort model that has the behavior you want (keeping the original table model the way it is), or you reimplement the source model. In both cases, have a look at the Editable Tree Model (http://doc.trolltech.com/latest/itemviews-editabletreemodel.html) example. If you want to use the sort/proxy model approach, have a look at the KDE libraries. KDE is Qt based and they have some developers that are quite good at creating sort/filter proxy models.

stefanadelbert
6th December 2010, 04:11
Thanks for the reply, franz. I like the idea of implementing a sort proxy so that the underlying table model remains intact.

I'm happy to take a look at the KDE libraries. Any chance you could point me at some code in particular that would be relevant? I use Gnome, so I'm unfamiliar with KDE and KDE apps that might have an implementation similar to what I'm after.

Many thanks
Stefan

franz
6th December 2010, 13:01
Maybe check this article about an identity proxy model (http://steveire.wordpress.com/2010/08/25/kidentityproxymodel-does-nothing-fast/) by one of the kde guys. You might want to check that out and see if it gives you a nudge in the right direction.

stefanadelbert
7th December 2010, 00:22
Thanks, Franz.

I spent some time last night going through the KDEUI lib and I found the KIdentityProxyModel and the KCategorizedSortFilterProxyModel, but unfortunately I don't see how they are going to help me yet. But they have given me some ideas.

I have an underlying CAbstractListModel with an attached QTableView. What I'd like to do is view the data from a list model in a QTreeView with arbitrary grouping defined by the user. The user should be able to specify a column (only one at a time, i.e. no nested grouping) to group by and then each unique value in that column would become a new parent node with the relevant original rows are children. Also the "group by" column would be hidden. I found an working online ASP.NET example of the kind of functionality I'm talking about here.

Maybe what I need to do is have a proxy model (inheriting from QSortFilterProxyModel perhaps) on top of the list model which provides extra functionality that is required by the QTreeView, e.g.

QModelIndex parent(const QModelIndex &index) const;

I want to create a generic solution because I have a few tables in my application that would benefit from this functionality. Unfortunately the priority of this job is not high at all so I'm not able to give it too much time right now. I'll post back with further ideas, progress, etc.

stefanadelbert
7th December 2010, 06:15
I did some more searching and found this wiki entry, Transpose_proxy_model.

I might just be getting closer to a possible answer. Having a specialised QAbstractProxyModel that is able to provide a QTreeView with the relevant hierarchical information could work. The trick is going to be working out the hierarchical details from a non-hierarchical model.

I'm going to have to just try it out and see what happens.