PDA

View Full Version : How to map a table model to a tree with QAbstractProxyModel?



l034n
10th June 2010, 19:10
As the title says, I have a model class derived from QAbstractTableModel. I can display it correctly on a QTableView or QListView. However, whenever I try to make a proxy model to display it in a QTreeView I either end up doing some pretty nasty stuff (like, keeping the row number of a model as internalId, assigning different Ids to the 'virtual nodes so I can distinguish them etc). At the end, I usually end up not using the MVC at all because of this. I have tried many times to make a good clean solution at no avail.
So, what I need is this. Let's say I have a table model with columns like this (pretty simplified though):

id | first_name | last_name | city | job_position

now, I sometimes need to display it in a table, or a list which is fine. But many times I need to display it in a tree, where the cities will be top nodes and will contain everyone in that city. Or maybe the job positions will be as top nodes. You get the idea. I've been looking for a clean solution to this all over and decided to post it here before going back to not using MVC in the current project. I hope someone can help here.

Thank you

ChrisW67
11th June 2010, 09:14
There's something similar to this in the history management of the browser example. It is also described briefly here: http://labs.trolltech.com/blogs/2008/03/25/advanced-example-of-modelview/