<?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[Delegate for QSqlTableModel &#x2F; QTableView - problem tabbing from one row to next when editing]]></title><description><![CDATA[<p dir="auto">Good evening,</p>
<p dir="auto">In order to learn more about database handling and Qt, I have set up a simple database in mysql and I am working on being able to edit existing and new records. It is working, but the behaviour is strange when I tab out of the row and into the next. If I press return while still in the row being edited, everything works as planned. If I tab out of the last field into the next row, it picks up the data from the next row and stores it. First of all, I had a suspicion this is a delegate issue, as I had the QSqlRelationalTableDelegate set up originally. I have subsequently removed the setDelegate() call, hoping that default behaviour would be correct. Alternatively, do I need to set up a signal/slot to catch the movement out of the row, then update the database?</p>
<p dir="auto">Here is the code:</p>
<p dir="auto">nametablewindow.h:<br />
@<br />
#ifndef NAMETABLEWINDOW_H<br />
#define NAMETABLEWINDOW_H</p>
<p dir="auto">#include &lt;QtGui&gt;<br />
#include &lt;QtSql&gt;</p>
<p dir="auto">enum {      // Field names<br />
names_id = 0,<br />
names_firstname = 1,<br />
names_surname = 2,<br />
names_address_id = 3,<br />
names_comment = 4<br />
};</p>
<p dir="auto">class NameTableWindow: public QWidget<br />
{<br />
Q_OBJECT<br />
public:<br />
NameTableWindow(QWidget *parent = 0);<br />
//virtual bool insertRows(int row, int count, const QModelIndex &amp; parent = QModelIndex());<br />
signals:</p>
<p dir="auto">public slots:<br />
void insert(void);<br />
void remove(void);</p>
<p dir="auto">private:<br />
QSqlTableModel *namesModel;</p>
<pre><code>QTableView *namesView;
QLabel *namesLabel;
QDialogButtonBox *buttonBox;
</code></pre>
<p dir="auto">};</p>
<p dir="auto">#endif // NAMETABLEWINDOW_H<br />
@</p>
<p dir="auto">nametablewindow.cpp<br />
@<br />
#include "nametablewindow.h"</p>
<p dir="auto">NameTableWindow::NameTableWindow(QWidget *parent) :<br />
QWidget(parent)<br />
{<br />
// Create model and load data query<br />
namesModel = new QSqlTableModel(this);<br />
namesModel-&gt;setTable("names");<br />
namesModel-&gt;setEditStrategy(QSqlTableModel::OnRowChange);</p>
<pre><code>// Set up the headers
namesModel-&gt;setHeaderData(names_id,Qt::Horizontal, tr("id"));
namesModel-&gt;setHeaderData(names_firstname, Qt::Horizontal, tr("First Name"));
namesModel-&gt;setHeaderData(names_surname,Qt::Horizontal, tr("Surname"));
namesModel-&gt;setHeaderData(names_address_id, Qt::Horizontal, tr("address id"));
namesModel-&gt;setHeaderData(names_comment, Qt::Horizontal, tr("Comment"));

// Load the table data
namesModel-&gt;select();

// Create the view
namesView = new QTableView(this);
namesView-&gt;setModel(namesModel);
namesView-&gt;setEditTriggers(QAbstractItemView::AnyKeyPressed
                           | QAbstractItemView::DoubleClicked);

// Removed - this delegate is for QSqlRelationalTableModel
//namesView-&gt;setItemDelegate(new QSqlRelationalDelegate(this));

namesView-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
namesView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows); // select row not cell
namesView-&gt;hideColumn(names_id);
namesView-&gt;resizeColumnsToContents();
namesView-&gt;horizontalHeader()-&gt;setStretchLastSection(true);

// Set up Label
namesLabel = new QLabel(tr("Names in Table"));
namesLabel-&gt;setBuddy(namesView);

QVBoxLayout *layout = new QVBoxLayout;

layout-&gt;addWidget(namesLabel);
layout-&gt;addWidget(namesView);

connect(namesView, SIGNAL(activated(QModelIndex)),  namesModel, SLOT(submitAll()));

buttonBox = new QDialogButtonBox;
QPushButton *addButton = buttonBox-&gt;addButton(tr("Add"),QDialogButtonBox::ActionRole);
QPushButton *rmButton = buttonBox-&gt;addButton(tr("Delete"),QDialogButtonBox::ActionRole);
buttonBox-&gt;addButton(QDialogButtonBox::Ok);

layout-&gt;addWidget(buttonBox);

connect(rmButton, SIGNAL(clicked()), this, SLOT(remove()));
connect(addButton, SIGNAL(clicked()), this, SLOT(insert()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(close())); // SLOT(accept()));

setLayout(layout);
setWindowTitle(tr("Names Table"));

namesView-&gt;setCurrentIndex(namesModel-&gt;index(0,0));
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void NameTableWindow::insert()<br />
{<br />
int row = namesView-&gt;currentIndex().row();</p>
<pre><code>if (row &lt; 0) // no record selected - add to end
    row = namesModel-&gt;rowCount();

namesModel-&gt;insertRows(row,1);
qDebug("rowCount(): %d", row);

QModelIndex index = namesModel-&gt;index(row,0);
namesView-&gt;setCurrentIndex(index);
namesView-&gt;edit(index);
</code></pre>
<p dir="auto">}<br />
void NameTableWindow::remove()<br />
{<br />
int row = namesView-&gt;currentIndex().row();<br />
namesModel-&gt;removeRow(row);<br />
}<br />
@</p>
<p dir="auto">...and for completion, main.cpp:<br />
@<br />
#include &lt;QApplication&gt;<br />
#include "mainwindow.h"<br />
#include "database.h"<br />
#include &lt;iostream&gt;<br />
#include "nametablewindow.h"</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QApplication a(argc, argv);</p>
<pre><code>// The database connection needs to be made before any of the models are set
// up, otherwise you will get "Database not open" errors and no data

if (!createConnection()) {
    std::cerr &lt;&lt; "Unable to open database" &lt;&lt; std::endl;
    return 1;
}

NameTableWindow namesWindow;
namesWindow.show();

return a.exec(&amp;#41;;
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">I can post the remaining files if you want to have a go with running it yourself.</p>
<p dir="auto">I am sure I am missing something simple - I look forward to your help!</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.qt.io/topic/21430/delegate-for-qsqltablemodel-qtableview-problem-tabbing-from-one-row-to-next-when-editing</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 20:05:40 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/21430.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Nov 2012 08:42:37 GMT</pubDate><ttl>60</ttl></channel></rss>