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. QSqlTableModel and QTableView: table headers not shown.

QSqlTableModel and QTableView: table headers not shown.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.5k 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.
  • S Offline
    S Offline
    simozz
    wrote on last edited by simozz
    #1

    Hi all,

    I am trying to implement a small database handler GUI based on the QSqlTableModel and QTableView. My first test are based on the following docs by QT:

    https://doc.qt.io/qt-5/qsqltablemodel.html
    https://doc.qt.io/qt-5/sql-presenting.html

    An extract of the code I am testing is the following:

    /
    
    class DBHandlerWidget : public QWidget
    {
        Q_OBJECT
    
        private:
    
            QSqlDatabase database;
            QSqlQuery *query;
            QSqlTableModel *tableModel;
            QTableView *tableView;
            QVBoxLayout *vboxLayout;
    
        public:
    
            DBHandlerWidget(QWidget *parent = 0) 
                : QWidget(parent)
            {
                database = QSqlDatabase::addDatabase("QSQLITE");
                database.setDatabaseName("database.db");
                database.setHostName("localhost");
                if(!database.open())
                    QMessageBox::information(this, "Error", "Couldn't open database!");
                else {
                      query = new QSqlQuery(database);
                      query->exec(QLatin1String("create table equipments (id int primary key, ipaddress varchar(15), location varchar(50), serial varchar(32)"));
                      query->exec(QLatin1String("insert into equipments (ipaddress, location, serial) values(0, '10.1.0.1', 'MyLocation', '00001')"));
                
                      tableModel = new QSqlTableModel(this, database);
                      tableModel->setTable("equipments");
                      tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
                      tableModel->select();
                      tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("IP"));
                      tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Location"));
                      tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Serial"));
    
                      tableView = new QTableView;
                      tableView->setModel(tableModel);
                      tableView->show();
    
                      vboxLayout = new QvboxLayout(this);
                      vboxLayout->addWidget(tableView);
                 }
            }
    };
    

    The complete software compiles successfully but once the GUI is shown, in place of the QTableView I don't see hany table header.. Instead, I see an empty white space..

    I suppose that the solution should be trivial but I don't understand what could be wrong since I almost replicated the code from the examples to kick-off with this.

    Could someone give an hint to solve the problem ?
    Thank you in advance.

    Regards,
    S.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I'm not saying it's the solution but from the looks of it, you are missing one header data since you don't hide the id column.

      On a side note, there's no reason to keep a QSqlQuery object on the heap. They are usually created on stack at the moment of use.

      As I also already many times already: don't keep a private QSqlDatabase member variable. QSqlDatabase already handles these objects for you.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        I'm not saying it's the solution but from the looks of it, you are missing one header data since you don't hide the id column.

        On a side note, there's no reason to keep a QSqlQuery object on the heap. They are usually created on stack at the moment of use.

        As I also already many times already: don't keep a private QSqlDatabase member variable. QSqlDatabase already handles these objects for you.

        S Offline
        S Offline
        simozz
        wrote on last edited by
        #3

        Hello @SGaist

        @SGaist said in QSqlTableModel and QTableView: table headers not shown.:

        I'm not saying it's the solution but from the looks of it, you are missing one header data since you don't hide the id column.

        Thanks for advising on this.
        However, nothing changes.

        Also, the database.db it's created but remains empty.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If it's empty then it explains why there are no headers.

          You should always check the result of the queries you run and that is missing from your code.

          The database file creation is not necessarily a sign of anything. It's the default behaviour of SQLite. When the file path given to it points to a file that does not exists, it creates it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • S Offline
            S Offline
            simozz
            wrote on last edited by simozz
            #5

            I think I have kicked-off this.

            This line of the previous code:

            query->exec(QLatin1String("create table equipments (id int primary key, ipaddress varchar(15), location varchar(50), serial varchar(32)"));
            

            Is wrong. ;-)

            BTW this is an extract of what I achivied until now:

            database = QSqlDatabase::addDatabase("QSQLITE");
            database.setDatabaseName("database.db");
            if(database.isValid())
            {   
                database.open();
                if(database.isOpen())
                {
                    QSqlQuery *query = new QSqlQuery(database);
                    query->exec(QLatin1String("CREATE TABLE equipments (id INTEGER PRIMARY KEY, ipaddress TEXT NOT NULL, location TEXT NOT NULL, serial TEXT NOT NULL);"));
                    // query->exec(QLatin1String("insert into equipments (id, ipaddress, location, serial) values(?, ?, ?);"));
            
                    /*query->addBindValue("0");   
                      query->addBindValue("10.1.0.1");
                      query->addBindValue("MyLocation");
                      query->addBindValue("abcdefgh");
                      query->exec();*/
            
            
            
                    database.commit();
                }
                else
                    QMessageBox::information(this, "Error", "Couldn't open database!" +  database.lastError().text());
            }
            
            tableModel = new QSqlTableModel(this);
            tableModel->setTable("equipments");
            tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
            tableModel->setHeaderData(ID, Qt::Horizontal,QObject::tr("ID"));
            tableModel->setHeaderData(IP, Qt::Horizontal,QObject::tr("IP"));
            tableModel->setHeaderData(LOC, Qt::Horizontal, QObject::tr("Location"));
            tableModel->setHeaderData(SN, Qt::Horizontal, QObject::tr("Serial"));
            tableModel->select();
            
            tableModel->insertRows(0, 1);
            tableModel->setData(tableModel->index(0, ID), 0);
            tableModel->setData(tableModel->index(0, IP), "10.0.0.1");
            tableModel->setData(tableModel->index(0, LOC), "MyHome");
            tableModel->setData(tableModel->index(0, SN), "abde767");
            tableModel->submitAll();
            
            tableView = new QTableView;
            tableView->setModel(tableModel);
            tableView->show();
            tableView->setSelectionMode(QAbstractItemView::SingleSelection);
            tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
            // tableView->setColumnHidden(ID, true);
            tableView->resizeColumnsToContents();
            tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
            tableView->horizontalHeader()->show();
            

            Perhaps there are better ways to start the tableModel, but now I am able to view the header and the content !

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              As suggested, you should check the error you get from QSqlQuery to see that exactly you are doing wrong with it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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