Results 1 to 6 of 6

Thread: QSqlRelationTableModel - how to obtain foreign key value

  1. #1
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlRelationTableModel - how to obtain foreign key value

    I have QSqlRelationTableModel and need to get foreign key value fo record not the value from related table. Is this possible with Qt classes ?

  2. #2
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationTableModel - how to obtain foreign key value

    This method may help you:

    Qt Code:
    1. QSqlTableModel * QSqlRelationalTableModel::relationModel ( int column ) const
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationTableModel - how to obtain foreign key value

    Quote Originally Posted by yuriry View Post
    This method may help you:

    Qt Code:
    1. QSqlTableModel * QSqlRelationalTableModel::relationModel ( int column ) const
    To copy to clipboard, switch view to plain text mode 
    I know this method but how can I obtain correct record from this model ?

  4. #4
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationTableModel - how to obtain foreign key value

    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.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationTableModel - how to obtain foreign key value

    Quote Originally Posted by yuriry View Post
    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:
    1. QSqlRelationalTableModel model( parent, db);
    2. model.setTable("people");
    3. model.setRelation( 2,QSqlRelation("town", "id","name") );
    To copy to clipboard, switch view to plain text mode 

    SELECT statement looks like :

    Qt Code:
    1. SELECT "people"."name","people"."surname","relTblAl_3"."name" FROM "people","towns" "relTblAl_3" WHERE ("people"."town_id"="relTblAl_3"."id") ORDER BY "people"."name" ASC
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lesiok for this useful post:

    yuriry (8th October 2008)

  7. #6
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationTableModel - how to obtain foreign key value

    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...

Similar Threads

  1. How to get list of database's table's foreign keys?
    By jambrek in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2008, 05:50
  2. Nullable foreign keys in QTableView
    By Banjo in forum Newbie
    Replies: 3
    Last Post: 31st January 2008, 22:07
  3. FOREIGN KEY + QComboBox
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2007, 10:37
  4. Model-Views: How to obtain QTreeWidgetItem from QModelIndex
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2006, 20:37
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 22:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.