View Full Version : Model / View - Design Question

5th June 2009, 00:25
Hi !

I have a tree "domain model" with a lot of different nodes.
Access to the domain model is done through QAbstractItemModel which acts more like an adapter to the domain model.
The views (tree view and table view - like an explorer) are attached through two proxy models to the adapter.
The problem is that the table view shows a lot of different data (but partially the same as the tree view) and this depends on the selected node. Unfortunately setRootIndex() which is used to select what has to be shown in the table view doesn't provide something to adjust the header of the table view (the header/data should come from the item set by setRootIndex() and not the model itself).
Providing all possible headers and filtering through a proxy (depending on the node type) is complicated for such a lot of different data/headers.
Using different Q...Models (one always attached for the tree and one dynamically attached to the selected node) attached to the domain model would imply that I have to do the synchronisation by my self (That's not what I want, when Qt provides this already)

It looks like a sub-model (table) within the model (tree).
So what's the best solution for that problem ?


tree view
...+--- node11
...+--- node12
.............+--- node 121

table view node1 selected:
name | h1 | h2 | h3 | h4
node11 | x1 | x2 | x3 | x4
node12 | y1 | y2 | y3 | y4

table view node 121 selected:
name | h1 | h7
node121 | a1 | a7

.... (lets assume 30 different nodes types, and a lot of different columns)

many thanks for your help,

Note: mixing up the tree content with the table content looks like a little bit of violation of "separation of concernce" to me. nevertheless - it's legacy code and can't be changed.

5th June 2009, 08:38
So you want to have different nodes with different number of columns and headings ?

5th June 2009, 11:24
That's exactly what I want.
Selecting a node in the (left) tree shows the content (right) in the table. And there are a lot of different nodes. Each node type has different header and data.

5th June 2009, 23:21
Solved with two different Q...Models.
click on the item in the tree view prepares a model and view in the table view,
depending on the node type.
synchronisation must be implemented separate.

best regards,

6th June 2009, 01:16
To me it seems a simple proxy and manipulation with QAbstractItemView::rootIndex() would be sufficient...

7th June 2009, 19:20
I did an implementation with two proxies, but it was really ugly.
- I had to analyse the content of the nodes for the differnt views (specially requirement).
- You can't create an index for the source model (you must store it, which results in a lot of duplicated QModelIndexes, or you have to implement a workaround, to be able to set the model pointer correct in the index). for a huge amout of data not really nice.
- The nodes knows the header and with proxies you have to set the headers far away from the nodes.

Returning a lot of different columns in one model (all available columns for all nodes) and then filtering out what's necessary is painful.

suppose (a concrete example - the real application is much more painful):


where the headers are
room: floor, size, costs, ............
device: supplier, pin-count, supply-voltage, .......
pin: mode, trigger-voltage, period, ...........

it's different from file browser or something else, where you show more or less the
same data for each node. one model with one header.

best regards,

7th June 2009, 22:35
Aren't you overcomplicating things? Try to break your functionality into fundamental properties and then start thinking about the architecture of the model. If you have to store indexes instead of calculating them then it means your design is wrong.

8th June 2009, 00:53
How to calulate the source model index from the proxy model index (the other direction is no problem). Especially for a tree model ? Seems this problem have other people too :


00055 namespace {
00056 // Think this is ugly? Well, it's not from me, it comes from QProxyModel
00057 struct KDPrivateModelIndex {
00058 int r, c;
00059 void *p;
00060 const QAbstractItemModel *m;
00061 };
00062 }

And make a static_cast :-(
(There is another link, where it's better documented).

I can't think about restructuring the domain model. This is legacy code and it must not be changed (software requirement). (by the way, I think it isn't changeable anyway. 15 years growing code - with a really huge database behind. And no one has money today. It's a shame :-)

I would appreciate to know a good source about proxies using Q...Model, or when you can post something.

best regards,

Nevertheless: QT is the best GUI framework I've ever seen.

8th June 2009, 08:39
If someone bases his code on deprecated QProxyModel then I have no comments on that :)

As for your situation - I mean the design of the internals of the model, not what it represents.