I have QSqlRelationTableModel and need to get foreign key value fo record not the value from related table. Is this possible with Qt classes ?
I have QSqlRelationTableModel and need to get foreign key value fo record not the value from related table. Is this possible with Qt classes ?
This method may help you:
Qt Code:
To copy to clipboard, switch view to plain text mode
After getting a related model you can obtain an index using QAbstractItemModel::index and data using QAbstractItemModel::data. I hope the row number is the same as in the original model.
Unfortunately no. QSqlRelationalTableModel::relationModel represents related table in database and it have number of rows equel to number of records in this table.
I think that this is not trivial problem because SELECT statement constructed in QSqlRelationalTableModel::selectStatement method remove all columns which are foreign keys to another tables.
In example.
Main table people with columns name, surname, town_id.
Related table towns with columns name, id.
After :
Qt Code:
model.setTable("people");To copy to clipboard, switch view to plain text mode
SELECT statement looks like :
Qt Code:
SELECT "people"."name","people"."surname","relTblAl_3"."name" FROM "people","towns" "relTblAl_3" WHERE ("people"."town_id"="relTblAl_3"."id") ORDER BY "people"."name" ASCTo copy to clipboard, switch view to plain text mode
yuriry (8th October 2008)
Yeah, you are right, it might not be possible to get foreign key(unless the display column is also unique so that you can search the related model, but this is not a very good option). Frankly, I do not even use standard models while working with databases, only custom ones sub-classed from QAbstractItemModel...
Bookmarks