1

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?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Paul
  • 2,474
  • 7
  • 33
  • 48
  • 1
    That there's no magic going on; SQLite does not push any notifications to Qt in order to have such views to update. – peppe Jan 26 '15 at 00:25
  • 2
    possible duplicate of [Auto-refresh QSqlTableModel/QTableView when the underlying SQL table is changed from outside of my application](http://stackoverflow.com/questions/26365301/auto-refresh-qsqltablemodel-qtableview-when-the-underlying-sql-table-is-changed) – sashoalm Jan 26 '15 at 09:09
  • A timer doing a select query on timeout event could do the trick and doesn't come that costly. – Alireza Soori Jan 27 '15 at 09:18

1 Answers1

2

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.

Community
  • 1
  • 1
Nejat
  • 31,784
  • 12
  • 106
  • 138