Results 1 to 2 of 2

Thread: how to add sub table to QSqlRelationalTableModel

  1. #1
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post how to add sub table to QSqlRelationalTableModel

    Hi All,

    I would like to add a column on my display table to show the population of the city (see code)
    I modified the QT example and added the item "population" to the city table.

    Does anybody knows how to add this column to the tableview (model)
    is there a setRelation to add an extra column??

    Thanks
    SunnySan

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QApplication>
    4.  
    5.  
    6. bool createConnection()
    7. {
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setHostName("127.0.0.1");//for localhost loop
    10. db.setDatabaseName("local_DB"); //if the dababase does not exist he will create it
    11. //the database name is also the name of the file in the working directory
    12.  
    13. if (!db.open()) {
    14. QMessageBox::critical(0, QObject::tr("Database Error"),
    15. db.lastError().text());
    16. return false;
    17. }
    18. return true;
    19. }
    20.  
    21. void initializeModel(QSqlRelationalTableModel *model)
    22. {
    23. model->setTable("employee");
    24.  
    25. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    26. model->setRelation(2, QSqlRelation("city", "id", "name"));
    27. model->setRelation(3, QSqlRelation("country", "id", "name"));
    28.  
    29. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    30. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    31. model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
    32. //Pb
    33. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Population Of City")); //PROBLEM Here
    34. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Country"));
    35.  
    36. model->select();
    37. }
    38.  
    39. QTableView *createView(const QString &title, QSqlTableModel *model)
    40. {
    41. QTableView *view = new QTableView;
    42. view->setModel(model);
    43. view->setItemDelegate(new QSqlRelationalDelegate(view));
    44. view->setWindowTitle(title);
    45. return view;
    46. }
    47.  
    48. void createRelationalTables()
    49. {
    50. QSqlQuery query;
    51. query.exec("create table employee(id int primary key, name varchar(20), city int, country int)");
    52. query.exec("insert into employee values(1, 'Espen', 5000, 47)");
    53. query.exec("insert into employee values(2, 'Harald', 80000, 49)");
    54. query.exec("insert into employee values(3, 'Sam', 100, 1)");
    55.  
    56. query.exec("create table city(id int, name varchar(20),population varchar(20))");
    57. query.exec("insert into city values(100, 'San Jose','200,000')");
    58. query.exec("insert into city values(5000, 'Oslo','200,020')");
    59. query.exec("insert into city values(80000, 'Munich','200,110')");
    60.  
    61. query.exec("create table country(id int, name varchar(20))");
    62. query.exec("insert into country values(1, 'USA')");
    63. query.exec("insert into country values(47, 'Norway')");
    64. query.exec("insert into country values(49, 'Germany')");
    65. }
    66.  
    67. int main(int argc, char *argv[])
    68. {
    69. QApplication app(argc, argv);
    70. if (!createConnection())
    71. return 1;
    72. createRelationalTables();
    73.  
    74.  
    75. initializeModel(&model);
    76.  
    77. QTableView *view = createView(QObject::tr("Relational Table Model"), &model);
    78. view->show();
    79.  
    80. return app.exec();
    81. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to add sub table to QSqlRelationalTableModel

    I guess the QSqlRelationalTableModel is only valid for simple tables.

    I will have to use the query select to combime and display several table (with child table having more than one item) together.

    If anybody find some good examples let me know

    thanks
    Sunnysan

Similar Threads

  1. Synchronize 2 models for 1 table
    By darkadept in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2008, 15:35
  2. Replies: 9
    Last Post: 21st June 2007, 10:27
  3. displaying any table on a qdatatable
    By Philip_Anselmo in forum Newbie
    Replies: 4
    Last Post: 9th May 2006, 22:12
  4. creating table plugin
    By mgurbuz in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 13:50
  5. Adding row in a Table after selected row
    By ankurjain in forum Qt Programming
    Replies: 3
    Last Post: 20th April 2006, 18:06

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
  •  
Qt is a trademark of The Qt Company.