PDA

View Full Version : QSqlTableModel or QSqlRelationTableModel



munna
11th March 2006, 13:47
Hi,

I have two tables called 'g_contacts' and 'name_d'. The fields in 'g_contacts' are

1. id
2. n_id(this field is 'id' field in name_d)
3. g_id.

Now, i want to select all the records in the table g_contacts with a particular g_id and then with the result of that select all the records in the table name_d where n_id(g_contacts field) is equal to id(name_d field).

How can i do this using sqltablemodel or sqlrealtiontablemodel ?

Thanks a lot

jrideout
12th March 2006, 19:10
try using setFilter ( QString )

This allows you to set the WHERE clause of a normal sql query

munna
13th March 2006, 04:37
Hi,

Thanks for replying.

My doubt is that can we use something like the example below as a filter ?

"name_d.id = g_contact.n_id and g_contact.g_id = 'some value'"

pls help

Thanks

jrideout
13th March 2006, 16:11
You can use QSqlQueryModel::setQuery(). If your object is an QSqlQueryModel it will be read only so will need to implement several function to be able to edit the data in a view. I tried to implement what you wanted with QSqlTableModel * QSqlRelationalTableModel::relationModel ( int column ) and Relations set, but I just got segfaults. It is a const thing of some sort you may want to try to figure it out or subclass QSqlRelationalTableModel. But for a just reading the data a QSqlQueryModel should be sufficient. For some very complex behavior I have in the past sublcassed QAbstractTableModel and used a series of QSqlRelationalTableModels as the way I stored data.