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