PDA

View Full Version : Questions regarding setting up a Qt SQL Model/View



Methedrine
22nd November 2007, 22:45
This is not the exact database I am working with, but a simplified version of it showing my main problem. So, let's assume we have the following SQL Database structure:


Table A - a list of employees:
id - primary key
name - string describing a persons name

Table B - a list of offices:
id - primary key
location - string describing an office location of a person from table a

Table C - a list of company cars:
id - primary key
car - string describing the type of car of a person from table a

Table A_B_Relation - each person can belong to multiple offices:
a_id - foreign key to table A.id
b_id - foreign key to table B.id

Table A_C_Relation - each person can own several company cars:
a_id - foreign key to table A.id
c_id - foreign key to table C.id

The displayed overview of people and cars they own/offices they belong to should look like the attached image.

The main problem that bugs me is: How to set up the QSqlRelationalTableModel properly?
When I set it up using table A as main table then I fail to find a way of displaying the car / office names - QSqlRelation only allows me to display the IDs of each role, or did I miss something in the docs about using it for this kind of thing? Currently I think that I will have to write my own model - am I correct there? It is certainly possible to simply use a QSqlTableModel and set a custom sql query on it which would deliver the data in a way that I could display it as I want using standard Qt classes.
But then there comes the problem with editing the displayed data in a proper way. I assume that safely editing the fields would require a custom delegate, no?

I am a bit puzzled if I am trying the correct approach on this issue.

Methedrine
23rd November 2007, 14:19
So, I contemplated a bit more about my problem. Since all my tables are actually models on their own my main problem boils down to combining data from different models in a single view.

Thus I thought that QAbstractProxyModel derivates should be sufficient for what I want to do, but I do not see an option to map between two different models with it?

jacek
25th November 2007, 23:37
Thus I thought that QAbstractProxyModel derivates should be sufficient for what I want to do, but I do not see an option to map between two different models with it?
You can set only one source model for QAbstractProxyModel, but you can subclass QAbstractItemModel to create multi-source proxy.

What you want to achieve is not exactly a table, because it displays a hierarchy, so maybe it will be easier if you use QTreeView?

Methedrine
26th November 2007, 09:26
You can set only one source model for QAbstractProxyModel, but you can subclass QAbstractItemModel to create multi-source proxy.

I guess that is what I am looking for, thanks for pointing it out. I was looking so much at models that I must have somehow missed the forest with seeing all those trees :D



What you want to achieve is not exactly a table, because it displays a hierarchy, so maybe it will be easier if you use QTreeView?

Actually it must be a table view, that's one of the requirements. But yes, it certainly resembles some kind of hierarchy. I will keep you people update as I progress on my journey :)