Paul
Paul

Reputation: 2564

QSqlDatabase and QTableView do not sync( SQLite)

I am using an SQLite database with QSqlDatabase and show data using 2 QTableView with QSqlTableModels.

My application inserts data into tables using QSqlQuery but the tables do not update to display new data (the no of rows displayed are not changed for example). I was expecting new data to be automatically reflected in the QTableView.

What am I doing wrong here?

Upvotes: 1

Views: 812

Answers (1)

Nejat
Nejat

Reputation: 32635

This feature is not supported by Qt. When a table is altered, there is no notification mechanism to update the model and view automatically. Although you can do it in PostgreSQL in some way as stated here but in SQLite you need to query the database periodically and update the view which is costly. An other option is to use QFileSystemWatcher to catch file changes and update the view on time:

QFileSystemWatcher *databaseWatcher = new QFileSystemWatcher(this);
databaseWatcher->addPath("Path/To/database.sqlite");
QObject::connect(databaseWatcher, SIGNAL(fileChanged(QString)), this, SLOT(refreshView()));

Now it watches the database file and when ever it's changed by an external process or by your self, fileChanged signal is emitted and refreshView slot is called in which you should update the model and view.

Upvotes: 2

Related Questions