Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-134432

QSqlTableModel::beforInsert() does not work

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.8
    • SQL Support
    • None
    • All

    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

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            chehrlic Christian Ehrlicher
            chehrlic Christian Ehrlicher
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes