At line 44 the row count is 6 (not 5) so Qt is keeping the new row up to this point. I initially suspected the issue was purely that you modify the row and do not provide a valid value for the foreign key column (i.e. a 1, 2, or 3). Even with this value set, I could not make it work using the QSqlRecord approach.
However, the behaviour is as expected with this code using the model's setData() method:
qDebug() << "immediately after insertRow()" << tableModel.rowCount();
tableModel.setData(tableModel.index(0, 1),
tableModel.setData(tableModel.index(0, 2), 3);
qDebug() << "immediately before submitAll()" << tableModel.rowCount();
qDebug() << "immediately after insertRow()" << tableModel.rowCount();
tableModel.setData(tableModel.index(0, 1),
QLatin1String("item added by insertRow and setData"));
tableModel.setData(tableModel.index(0, 2), 3);
qDebug() << "immediately before submitAll()" << tableModel.rowCount();
To copy to clipboard, switch view to plain text mode
This approach also fails if the foreign key column is not populated.
Bookmarks