1

Data base is oppened in application by following code:

db = QSqlDatabase::addDatabase("QSQLITE");
bool dbExists = QFile::exists("base.db");
db.setDatabaseName("base.db"); // ":memory:"
if (!db.open()) {
    db.close();
    QMessageBox::critical(0, tr("Cannot open database"),
                          tr("Unable to establish a database connection.\n"
                             "This example needs SQLite support. Please read "
                             "the Qt SQL driver documentation for information how "
                             "to build it."), QMessageBox::Cancel);
    return;
}
if (!dbExists)
    createDB();

I've mapped some tables to widgets via QDataWidgetMapper. All work fine during application run. Changes take effect. But all the chages are lost after restarting application with existing base.db. Application starts much faster, so it opens this file but with unchanged data base.

I've tried to close application by

db.close();
this->close();

There is no luck. Even the sync query did not help after db.open:

QSqlQuery query(db);
query.exec("PRAGMA synchronous = ON;");

What have I missed to sync changes to base.db?

UPDATE. You can try to build sqlwidgetmapper standard example and change :memory: to "base.db" file. It doesn't save changed data to file too.

1
  • Please add the code showing how you set up the QWidgetDataMapper and the model it uses. I made a wild guess at the problem, but even if I guessed right and that solves the problem, please add the code for the benefit if others. Commented Sep 21, 2016 at 19:38

1 Answer 1

2

Problem is possibly manual submit policy.

Either switch that to auto, or manually call QDataWidgetMapper::submit() at appropriate time (before new value is lost from the widget).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, hyde. OnFieldChange works just fune. There is no OnAutoSubmit in QSqlTableModel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.