Blank Relational Table Model GUI
-
Hi,
I'd like to design a GUI based on the Relational Table Model provided with the examples. While I'm ok with Qt I'm new with all SQL related things, and I need to use a foreign key : based on the docs I think the table model is for me.
My purpose is to use the same example but I want to expand it thereafter to add more functionnalities. Thus instead of SQLlite I've used the MySQL access and defined a database as shown at the end.
While the request test have a correct answer in the debug console, why nothing is shown like the example does ? instead I've 2 blank windows.#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); if (!createConnection()) { qDebug() << "Not connected!"; } else { qDebug() << "Connected!"; /* Test of request : QSqlQuery query; query.exec("SELECT * FROM employee"); while (query.next()) { QString name = query.value(1).toString(); qDebug() << "name:" << name; } */ } QSqlRelationalTableModel model; initializeModel(&model); QTableView *view = createView(QObject::tr("Relational Table Model"), &model); view->show(); } void MainWindow::initializeModel(QSqlRelationalTableModel *model) { model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->setRelation(2, QSqlRelation("city", "CITY_ID", "name")); model->setRelation(3, QSqlRelation("country", "COUNTRY_ID", "name")); model->setHeaderData(0, Qt::Horizontal, QObject::tr("EMP_ID")); model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name")); model->setHeaderData(2, Qt::Horizontal, QObject::tr("City")); model->setHeaderData(3, Qt::Horizontal, QObject::tr("Country")); model->select(); } QTableView *MainWindow::createView(const QString &title, QSqlTableModel *model) { QTableView *view = new QTableView; view->setModel(model); view->setItemDelegate(new QSqlRelationalDelegate(view)); view->setWindowTitle(title); return view; } bool MainWindow::createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("qt_relationaltablemodel"); db.setUserName("root"); db.setPassword("root"); if (!db.open()) { qDebug() << "Database error occurred"; return false; } return true; } MainWindow::~MainWindow() { delete ui; }
======================================
Current database: qt_relationaltablemodel
+--------+--------+-------+---------+ | EMP_ID | name | city | country | +--------+--------+-------+---------+ | 1 | Espen | 5000 | 47 | | 2 | Harald | 80000 | 49 | | 3 | Sam | 100 | 1 | +--------+--------+-------+---------+ 3 rows in set (0.01 sec) mysql> select * from country ; +------------+---------+ | COUNTRY_ID | name | +------------+---------+ | 1 | USA | | 47 | Norway | | 49 | Germany | +------------+---------+ 3 rows in set (0.00 sec) mysql> select * from city ; +---------+----------+ | CITY_ID | name | +---------+----------+ | 100 | San Jose | | 5000 | Oslo | | 80000 | Munich | +---------+----------+ 3 rows in set (0.00 sec)
-
You should try to create your model in the heap (using the 'new' keyword) instead of creating it in the stack, and give it to your view.
Also, try to set your MainWindow's central widget and give your view and your model some parents, in order to avoid memory leaks. -
@ValentinMichelet
Well this just made the header visible but no db data in the table anyway