How to insert new record to QSqlTableModel with respect to default values of DB?
UPD:
I am trying to call pTableModel->insertRecord(0, pTableModel->record(0));
or pTableModel->insertRecord(0, pTableModel->record());
or pTableModel->insertRecord(QSqlDatabase::database().record("tableName"));
or pTableModel->insertRow(0);
Result the same, all fields are NULL instead of default values of DB.
The fact that you’re inserting at row 0 may be an issue. When I’ve tried doing that, I don’t get the record I expect when I later ask for the record at row 1 (which is where the inserted record should be after a call of
insertRecord(0, ...)). Also, are you callingpTableModel->submit()orpTableModel->submitAll()? If not, your record never gets committed to the database.This code works for me: