Strange things happening to QSqlTableModel (while inserting, deleting, updating)
Code:
model->setTable("subjects");
model->select();
model->removeColumn(0); // don't show the ID
ui->tableView->setModel(model);
Inserting:
Code:
model->insertRow(0);
Deleting:
Code:
QModelIndexList selection = ui->tableView->selectionModel()->selectedRows();
if (selection.count())
{
for (int i = 0; i < selection.count(); i++)
{
qDebug() << index.row();
qDebug() << model->removeRow(selection.at(i).row());
model->select();
qDebug() << model->lastError();
//model->submitAll();
}
//model1->submitAll();
}
So, the table displays correctly.
When I edit a cell, the row gets empty and shows !, like it was deleted. Nothing changes in DB.
When I delete a row with algorithm above, removeRow returns true, the table view blinks like it's re-rendering, but nothing changes.
When I insert a row, edit it, it gets empty and shows !, BUT it appears in DB and it's in the table when I reopen the app.
When I un-comment model->submitAll(); nothing changes.
Any ideas?
Re: Strange things happening to QSqlTableModel (while inserting, deleting, updating)
The deletion code is definitely wrong.
After the first modification all model indexes become invalid, so the loop continues with model indexes that are no longer appropriate.
It also reloads the data before submitting the change, that looks problematic to me as well.
Also the removal of a column looks strange. Did you want to use QTableView::setColumnHidden?
Cheers,
_
Re: Strange things happening to QSqlTableModel (while inserting, deleting, updating)
Quote:
Originally Posted by
anda_skoa
Also the removal of a column looks strange. Did you want to use QTableView::setColumnHidden?
It works now, thank you!
I was 100% sure I took that part from the docs but now I see it's
Code:
view->hideColumn(0); // don't show the ID
Anyway, thank you very much.
Added after 33 minutes:
I celebrated too soon, it fixed everything except:
Quote:
When I insert a row, edit it, it gets empty and shows !, BUT it appears in DB and it's in the table when I reopen the app.