Update QTableView after QSqlTableModel insert
-
I'm writing an application over SQLite3 database for home bookkeeping.
I'm using Qt 5.9.2, MinGW 5.3.0 32bit and QWidgets.Here I'm appending record to the QSqlTableModel (forgive me please for that kind of "ass-driven" development):
void RegistryTab::on_btnAdd_clicked() { AddEditDialog d(type, this); if(d.exec() == QDialog::Accepted) { QSqlField field("id",QVariant::String); field.setAutoValue(true); QSqlRecord t; t.append(field); QHash<QString,QString>::iterator i; for(i=d.data()->begin(); i != d.data()->end(); ++i) { QSqlField temp(i.key(), QVariant::String); t.append(temp); t.setValue(i.key(),i.value()); } modelsContainer->addRegistryRecord(type,t); } } void ModelsContainer::addRegistryRecord(RegistryType type, const QSqlRecord &record) { tableModels->value((*registryMap)[type])->insertRecord(-1, record); }
But after appending I have such a picture:
https://ibb.co/d1F66vHow can I refresh the QTableView to see model changes immediately?
Model's EditStrategy is "OnFieldChange".Thanks in advance!
-
Hi,
You should check the return value of insertRecord. Also, are you sure that your QSqlRecordObject contains something ?
-
After that code execution QTableView displays empty cells in a row with an exclamation mark on the left side.
But, if I restart the whole application, the data successfully appears => the data is inserted into underlying physical SQL table in database file.
Summing up, I cannot find any way to make QSqlTableModel data changes be immidiately displayed in a QTableView. -
AFAIR and from the SQL Presenting chapter, an exclamation mark means that the change was not submitted.
I'd try to call submitAll after insert to see if it changes anything.