PDA

View Full Version : getting entries from different sql tables into one model



SK
8th March 2006, 20:24
Hello everyone,
Nice forum you got here, hope you can lend me some of your expertise ;)
Unfortunately my source code is at work, where I don´t have a direct net connection, so I´ll try to put my problem into a few words.
I got a few SQL tables:

table address
ID City street nameRef
04 NY 5thAv 05
06 LA HBvd 06
09 SA whtever 08

table name
ID first sur
05 A. Peters
06 B. Jennings
08 C. Schlegel


What I want to have in the end is a table view showing me all this data in one table, e.g:
ID City street first sur
04 NY 5thAv A. Peters
06 LA HBvd B. Jennings
09 SA whtever C. Schlegel
(Bad formatting, but you should get the idea)

It´s easy to get one field from "table name" into "table address" by using QSqlRelationalTableModel and defining a relation. The problem now of course is, that I got 2 (and more in my real application) fields to be inserted instead of one. My first thought was to insert the number of needed columns, copy the "nameref" numbers into it and define as many relations as needed. This doesn´t work though, especially as I´m not allowed to edit the original database.
I hope I´m wrong on this one, looking forward to all replies.

wysota
8th March 2006, 20:34
Use QSqlQueryModel. Just remember it is a read-only model. If you want to make it writable, you have to subclass and implement that functionality yourself.

SK
9th March 2006, 21:23
Thanks, that actually worked fine. The only minor issue I´m having now is that the table view´s sortbyColumn function (activated via click on the respective header) doesn´t work anymore. Is there any function I have to implement in QSqlQueryModel that will give back this functionality? It worked fine with the QSqlTableModel I used before.

jrideout
9th March 2006, 21:48
try to link the click on the header to a custom slot which creates an "order by" part to your query and then runs it again. You will need to maintain a map of columns to column names though, to implement it.

wysota
9th March 2006, 23:22
Thanks, that actually worked fine. The only minor issue I´m having now is that the table view´s sortbyColumn function (activated via click on the respective header) doesn´t work anymore. Is there any function I have to implement in QSqlQueryModel that will give back this functionality? It worked fine with the QSqlTableModel I used before.

Use a proxy model (QSortFilterProxyModel or something like that).