Results 1 to 4 of 4

Thread: QSqlTableModel or QSqlRelationTableModel

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel or QSqlRelationTableModel

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel or QSqlRelationTableModel

    try using setFilter ( QString )

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

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel or QSqlRelationTableModel

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel or QSqlRelationTableModel

    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.

Similar Threads

  1. Trouble with "INSERT" by QSqlTableModel
    By AD in forum Qt Programming
    Replies: 12
    Last Post: 10th November 2008, 08:21
  2. QSqlTableModel slow even with manual submit
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2008, 20:40
  3. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  4. Replies: 4
    Last Post: 9th May 2008, 17:02
  5. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 26th January 2006, 07:58

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.