How to change record's fields in beforeInsert()?
-
The docs say:
void QSqlTableModel::beforeInsert(QSqlRecord &record)
This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.In my
QSqlRelationalTableModel
I added this code:connect(model, &QSqlRelationalTableModel::beforeInsert, this, [=](QSqlRecord &record) { record.setValue("idProgram", 123); // just as an example qDebug() << record; });
when running I add a new row using:
model->insertRow(model->rowCount()); QModelIndex index = model->index(model->rowCount() - 1, 0); if (!index.isValid()) return; ui->tableView->setCurrentIndex(index); ui->tableView->edit(index);
the debug output is correct:
QSqlRecord(3) 0: QSqlField("idProgram", int, tableName: "timelines", generated: yes, autoValue: false, readOnly: false) "123" 1: QSqlField("time", int, tableName: "timelines", generated: no, autoValue: false, readOnly: false) "0" 2: QSqlField("h", double, tableName: "timelines", generated: no, autoValue: false, readOnly: false) "0"
but the table view shows 0 instead of 123:
Is there anything I should do further in order to see the updated values in the cells?
-
@Mark81
I don't think it will make any difference, but for the sake of a two second change does it alter if you make your lambda use[&]
instead of[=]
?Does the behaviour alter if you use a
QSqlTableModel
instead of aQSqlRelationalTableModel
?You could subclass and override bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values). Presumably that shows the original, unchanged value?
-
@Mark81 said in How to change record's fields in beforeInsert()?:
Actually, hiding the column leads to the expected behavior: the correct value is stored into the db. I just wonder why I cannot see it leaving the column visible.
You posted this after my response. Presumably Qt isn't allowing correctly for changing the values during that slot? Do some sort of
update()
oremit dataChanged()
where you alter it? Check whether it's the same issue forQSqlTableModel
as viaQSqlRelationalTableModel
? -
-
See my comment in the bug report:
"After a first investigation I would say it worked for Qt4 but was broken in Qt5 with 48c68b05465e488020e241dd66290777b0eeeb3d in 2011 which leads to the question if this feature should not be declared broken and removed in Qt7... noone needed it for 14 years." -
@Christian-Ehrlicher if this feature is something that nobody uses, how would one fill the columns with default values when creating a new one? I'm not talking about
SQL
default values, but run-time generated. In my tables I'd use them quite often to pre-fill the columns with data that might be changed by the user, but usually does not.Examples:
- data/time
- expected values for foreign keys
- expected values by the context, etc...
-
@JonB said in How to change record's fields in beforeInsert()?:
@Mark81
Can't you create an empty initialQSqlRecord()
, fill certain columns withsetValue()
, present the record to the user for editing and theninsertRecord()
?As far as I understand, but please correct me if I'm wrong, your approach would work if you create something like a
QDialog
with the fields to be edited and submitted.Can you use the same approach while editing the table directly, using the
OnRowChange
edit strategy?In this case when you create a new row the record is already appended to the model (here the need of the
beforeInsert
). Then the user can edit it, but it is already inserted. -
@Mark81
I have not checked, but isn't the new row and its record only inserted into the in-memory model at this point, it has not yet been inserted into the backend database? You can still edit/change it in code, before the user performs any edits, and it is not committed until you move off that new row?