Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
6.8
-
None
Description
The following code does not work as expected, the values in the slot from signal QSqlTableModel::beforeInsert() are written to the database but not displayed:
int main(int argc, char* argv[]) { QApplication app(argc, argv); auto db = QSqlDatabase::addDatabase("QSQLITE"); qDebug() << db.open(); QSqlQuery q; qDebug() << q.exec("CREATE TABLE testtable (id integer, id2 integer)"); qDebug() << q.exec("INSERT INTO testtable (id, id2) VALUES (1, 2)"); QTableView tv; QSqlTableModel tm; tm.setTable("testtable"); tm.setEditStrategy(QSqlTableModel::OnRowChange); tm.select(); tv.setModel(&tm); QObject::connect(&tm, &QSqlTableModel::beforeInsert, [](QSqlRecord& record) { record.setValue("id", 4711); record.setGenerated("id", true); record.setValue("id2", 42); record.setGenerated("id2", true); }); tm.insertRow(tm.rowCount()); tm.submitAll(); q.exec("SELECT * from testtable"); while (q.next()) qDebug() << q.value(0) << q.value(1); tv.show(); int ret = app.exec();; q.exec("SELECT * from testtable"); while (q.next()) qDebug() << q.value(0) << q.value(1); return ret; }
Original report from the forum: https://forum.qt.io/topic/161246