PDA

View Full Version : implementing QAbstractProxyModel for a table -> tree proxy. technical / design qstn



roomzeig
1st July 2010, 21:55
Hi. I'm trying to implement a QAbstractProxyModel that will present a tree structure out of a flat table model.

In particular, rows of the source model are treated as entries, with columns specifying entry attributes. I would like the proxy to use entries' attributes to group the entries in a tree structure.

for example, given the following source table:

----------------------
| id | attr1 | attr2 |
----------------------
| 1 | a | x |
| 2 | a | y |
| 3 | b | z |
| 4 | a | z |
| 5 | b | x |
| 6 | b | y |
| 7 | c | z |
| 8 | a | x |
----------------------

... i wish to group the "leaves" (the ids) through attr1 at the first nesting level, and through attr2 at the second level:
a
- x
- - 1
- - 8
- y
- - 2
- z
- - 4
b
- y
- - 5
- - 6
- z
- - 3
c
- z
- - 7
Notice that in such way only the leaves of the proxy model tree have counterparts in the source model; branches appear only for grouping purpose.

My question concerns the implementation. Would i want to compute and store (and, well, update) a hashtable of index mappings (to be computed in QAbstractProxyMode::setSourceModel), or can anybody suggest a smart way of computing the mapping?

Also, if storing the mappings is the way to go, then it isn't clear to me how to properly map nested tree levels. Each QModelIndex is characterized by row, column and parent ModelIndex. However if i try to access the parent, the public method QModelIndex:: parent refers me back to the model: (m is the QModelIndex's QAbstractItemModel)


inline QModelIndex QModelIndex::parent() const
{ return m ? m->parent(*this) : QModelIndex(); }

If parents of QModelIndices are determined by the owning QAbstractItemModel, then given a QModelIndex, how do i determine what tree level it is at, inside of my custom proxy?!?

I'd greatly appreciate if anybody could suggest a smart implementation or resolve my circular parent problem.