<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QSqlTableModel and QTableView: table headers not shown.]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I am trying to implement a small database handler GUI based on the <strong>QSqlTableModel</strong> and <strong>QTableView</strong>. My first test are based on the following docs by QT:</p>
<p dir="auto"><a href="https://doc.qt.io/qt-5/qsqltablemodel.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qsqltablemodel.html</a><br />
<a href="https://doc.qt.io/qt-5/sql-presenting.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/sql-presenting.html</a></p>
<p dir="auto">An extract of the code I am testing is the following:</p>
<pre><code>/

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-&gt;exec(QLatin1String("create table equipments (id int primary key, ipaddress varchar(15), location varchar(50), serial varchar(32)"));
                  query-&gt;exec(QLatin1String("insert into equipments (ipaddress, location, serial) values(0, '10.1.0.1', 'MyLocation', '00001')"));
            
                  tableModel = new QSqlTableModel(this, database);
                  tableModel-&gt;setTable("equipments");
                  tableModel-&gt;setEditStrategy(QSqlTableModel::OnManualSubmit);
                  tableModel-&gt;select();
                  tableModel-&gt;setHeaderData(0, Qt::Horizontal, QObject::tr("IP"));
                  tableModel-&gt;setHeaderData(1, Qt::Horizontal, QObject::tr("Location"));
                  tableModel-&gt;setHeaderData(2, Qt::Horizontal, QObject::tr("Serial"));

                  tableView = new QTableView;
                  tableView-&gt;setModel(tableModel);
                  tableView-&gt;show();

                  vboxLayout = new QvboxLayout(this);
                  vboxLayout-&gt;addWidget(tableView);
             }
        }
};
</code></pre>
<p dir="auto">The complete software compiles successfully but once the GUI is shown, in place of the <strong>QTableView</strong> I don't see hany table header.. Instead, I see an empty white space..</p>
<p dir="auto">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.</p>
<p dir="auto">Could someone give an hint to solve the problem ?<br />
Thank you in advance.</p>
<p dir="auto">Regards,<br />
S.</p>
]]></description><link>https://forum.qt.io/topic/101647/qsqltablemodel-and-qtableview-table-headers-not-shown</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 03:38:12 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/101647.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Apr 2019 13:56:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSqlTableModel and QTableView: table headers not shown. on Thu, 11 Apr 2019 20:04:12 GMT]]></title><description><![CDATA[<p dir="auto">As suggested, you should check the error you get from QSqlQuery to see that exactly you are doing wrong with it.</p>
]]></description><link>https://forum.qt.io/post/522587</link><guid isPermaLink="true">https://forum.qt.io/post/522587</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 11 Apr 2019 20:04:12 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlTableModel and QTableView: table headers not shown. on Thu, 11 Apr 2019 13:02:12 GMT]]></title><description><![CDATA[<p dir="auto">I think I have kicked-off this.</p>
<p dir="auto">This line of the previous code:</p>
<pre><code>query-&gt;exec(QLatin1String("create table equipments (id int primary key, ipaddress varchar(15), location varchar(50), serial varchar(32)"));
</code></pre>
<p dir="auto">Is wrong. <strong>;-)</strong></p>
<p dir="auto">BTW this is an extract of what I achivied until now:</p>
<pre><code>database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("database.db");
if(database.isValid())
{   
    database.open();
    if(database.isOpen())
    {
        QSqlQuery *query = new QSqlQuery(database);
        query-&gt;exec(QLatin1String("CREATE TABLE equipments (id INTEGER PRIMARY KEY, ipaddress TEXT NOT NULL, location TEXT NOT NULL, serial TEXT NOT NULL);"));
        // query-&gt;exec(QLatin1String("insert into equipments (id, ipaddress, location, serial) values(?, ?, ?);"));

        /*query-&gt;addBindValue("0");   
          query-&gt;addBindValue("10.1.0.1");
          query-&gt;addBindValue("MyLocation");
          query-&gt;addBindValue("abcdefgh");
          query-&gt;exec();*/



        database.commit();
    }
    else
        QMessageBox::information(this, "Error", "Couldn't open database!" +  database.lastError().text());
}

tableModel = new QSqlTableModel(this);
tableModel-&gt;setTable("equipments");
tableModel-&gt;setEditStrategy(QSqlTableModel::OnManualSubmit);
tableModel-&gt;setHeaderData(ID, Qt::Horizontal,QObject::tr("ID"));
tableModel-&gt;setHeaderData(IP, Qt::Horizontal,QObject::tr("IP"));
tableModel-&gt;setHeaderData(LOC, Qt::Horizontal, QObject::tr("Location"));
tableModel-&gt;setHeaderData(SN, Qt::Horizontal, QObject::tr("Serial"));
tableModel-&gt;select();

tableModel-&gt;insertRows(0, 1);
tableModel-&gt;setData(tableModel-&gt;index(0, ID), 0);
tableModel-&gt;setData(tableModel-&gt;index(0, IP), "10.0.0.1");
tableModel-&gt;setData(tableModel-&gt;index(0, LOC), "MyHome");
tableModel-&gt;setData(tableModel-&gt;index(0, SN), "abde767");
tableModel-&gt;submitAll();

tableView = new QTableView;
tableView-&gt;setModel(tableModel);
tableView-&gt;show();
tableView-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
tableView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
// tableView-&gt;setColumnHidden(ID, true);
tableView-&gt;resizeColumnsToContents();
tableView-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);
tableView-&gt;horizontalHeader()-&gt;show();
</code></pre>
<p dir="auto">Perhaps there are better ways to start the tableModel, but now I am able to view the header and the content !</p>
]]></description><link>https://forum.qt.io/post/522509</link><guid isPermaLink="true">https://forum.qt.io/post/522509</guid><dc:creator><![CDATA[simozz]]></dc:creator><pubDate>Thu, 11 Apr 2019 13:02:12 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlTableModel and QTableView: table headers not shown. on Thu, 11 Apr 2019 12:44:04 GMT]]></title><description><![CDATA[<p dir="auto">If it's empty then it explains why there are no headers.</p>
<p dir="auto">You should always check the result of the queries you run and that is missing from your code.</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/522507</link><guid isPermaLink="true">https://forum.qt.io/post/522507</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 11 Apr 2019 12:44:04 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlTableModel and QTableView: table headers not shown. on Thu, 11 Apr 2019 12:30:11 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said in <a href="/post/522410">QSqlTableModel and QTableView: table headers not shown.</a>:</p>
<blockquote>
<p dir="auto">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 <code>id</code> column.</p>
</blockquote>
<p dir="auto">Thanks for advising on this.<br />
However, nothing changes.</p>
<p dir="auto">Also, the <strong>database.db</strong> it's created but remains empty.</p>
]]></description><link>https://forum.qt.io/post/522505</link><guid isPermaLink="true">https://forum.qt.io/post/522505</guid><dc:creator><![CDATA[simozz]]></dc:creator><pubDate>Thu, 11 Apr 2019 12:30:11 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlTableModel and QTableView: table headers not shown. on Wed, 10 Apr 2019 21:15:13 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">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 <code>id</code> column.</p>
<p dir="auto">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.</p>
<p dir="auto">As I also already many times already: don't keep a private QSqlDatabase member variable. QSqlDatabase already handles these objects for you.</p>
]]></description><link>https://forum.qt.io/post/522410</link><guid isPermaLink="true">https://forum.qt.io/post/522410</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 10 Apr 2019 21:15:13 GMT</pubDate></item></channel></rss>