Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the changes database via QSqlTableModel?

Tags:

c++

database

qt

I changed the value of cell in my Table:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sqlite.db");
db.open();

QSqlTableModel *model = new QSqlTableModel(0, db);
model->setTable("Expenses");
model->select();
model->setData(model->index(0,2), 100);

How now to save the changes in the database (sqlite.db)?

like image 915
Adam Shakhabov Avatar asked Oct 16 '25 15:10

Adam Shakhabov


1 Answers

As Qt docs says, saving changes depends on edit strategy settings of your model. Depending on the edit strategy, the value might be applied to the database at once or cached in the model. You should refer to the QSqlTableModel::setEditStrategy() function of your model and set up the one that corresponds to your needs. For example, if you set edit strategy to QSqlTableModel::OnFieldChange, all changes will apply immediately after setData() function call.

Otherwise, you simply need to call QSqlTableModel::submitAll() to submit all pending changes to the database.

like image 106
vahancho Avatar answered Oct 18 '25 05:10

vahancho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!