Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Blank Relational Table Model GUI
Forum Updated to NodeBB v4.3 + New Features

Blank Relational Table Model GUI

Scheduled Pinned Locked Moved General and Desktop
mysqltablemodel
3 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doubitchou
    wrote on last edited by doubitchou
    #1

    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)
    
    1 Reply Last reply
    0
    • ValentinMicheletV Offline
      ValentinMicheletV Offline
      ValentinMichelet
      wrote on last edited by ValentinMichelet
      #2

      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.

      D 1 Reply Last reply
      0
      • ValentinMicheletV ValentinMichelet

        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.

        D Offline
        D Offline
        doubitchou
        wrote on last edited by
        #3

        @ValentinMichelet
        Well this just made the header visible but no db data in the table anyway

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved