Results 1 to 5 of 5

Thread: QSqlRelationalTableModel QDataWidgetMapper

  1. #1
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlRelationalTableModel QDataWidgetMapper

    Hi,

    Here is my app in general:

    QTableView data list populated from database, the view is based on QSqlRelationalTableModel.
    Some columns in the view are displayed directly from the main table, some from related tables by foreign keys.

    Editing data is made by Dialog, I use QDataWidgetMapper to map model field to dialog widgets. Everything works fine.

    But for some reasons I want to get foreign key value in dialog for columns where foreign keys are replaced with i.e. name data (related to foreign key). Is there any method to do it?

    regards
    kawoN

  2. #2
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel QDataWidgetMapper

    Qt Code:
    1. QSqlRelation ( const QString & tableName, const QString & indexColumn, const QString & displayColumn )
    To copy to clipboard, switch view to plain text mode 

    The displayColumn is the name of the field to display instead of the foreign key.

  3. #3
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlRelationalTableModel QDataWidgetMapper

    OK, I think that maybe I didn't describe my problem clear.

    To simplify:
    QSqlRecord get by record() function from QSqlRelationalTableModel always (?) returns related value for column with foreign key - not original. Is there any method to obtain key value from record?

    For instance:

    Table 1:
    country_id country_name continent_id
    1 France 1
    2 Germany 1
    3 USA 2

    Table 2:
    continent_id continent_name
    1 Europe
    2 North America

    We construct QSqlRelationalTableModel where continent_id in Table_1 is replaced by continent_name from Table_2:

    QSqlRelationalTableMode *model = new QSqlRelationalTableModel;
    model->setTable("Table_1");
    model->setRelation(model->fieldIndex("continent_id"), QSqlRelation("Table_2", "continent_id", "continent_name"));

    Than when we want to read data (first row i.e.) from model we use QSqlRecord:
    ....
    QSqlRecord record = model->record(0);

    Than - if we want to get continent_id field (using value function):

    QString continent_id = record.value("continent_id").toString();

    we will get empty string. Only continent_name is available.
    My goal is to get continent_id, and I don't know how to do it.
    If you put record to qDebug() You will see that record doesn't contain continent_id field.

    regards
    kawoN
    Last edited by kawoN; 2nd March 2010 at 11:17.

  4. #4
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlRelationalTableModel QDataWidgetMapper

    I have the same problem...

  5. #5
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel QDataWidgetMapper

    Hi guys,

    same problem here.

    trying to execute a setRecord produces an error.
    It seems like in the QSqlRecord the id field is replaced by the description field.

Similar Threads

  1. Replies: 2
    Last Post: 29th December 2012, 14:14
  2. QDataWidgetmapper
    By locus in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2012, 11:24
  3. QDataWidgetMapper
    By rogerholmes in forum Newbie
    Replies: 4
    Last Post: 24th March 2009, 19:32
  4. QDataWidgetMapper and QSqlRelationalTableModel problem
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:46
  5. QDataWidgetMapper & QLabel
    By fpujol in forum Qt Programming
    Replies: 7
    Last Post: 12th November 2007, 11:30

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.