I have a SQLite database. I am trying to capture events on changes in the database in a Qt program that uses QtSql module, e.g., when a new record is inserted in a table from outside the Qt program. QSqlDatabase class offers a function to subscribe to the notifications, however there is no example in the Qt framework that illustrates how to setup and capture the events. I adopted (after some search on Internet) the following approach, but it is not working and I could not find why.
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("file.db");
if (!db.isOpen()) {
qFatal("Failed to open database");
}
if (db.driver()->hasFeature(QSqlDriver::EventNotifications)) {
db.driver()->subscribeToNotification("IenState"); //"IenState" is table name
QObject::connect(db.driver(), SIGNAL(notification(const QString &)),
this, SLOT(procEvents(const QString &)));
}
else {
qFatal("Driver does NOT support database event notifications");
}
//slot function
void DbSqlite::procEvents(const QString &name)
{
qWarning("%s: %s", __func__, qPrintable(name));
if (name.compare("IenState", Qt::CaseSensitive) == 0) {
qWarning("%s: from IenState table", __func__);
}
}
The procEvents slot function is never get called. It looks like the notification signal is never fired from the db.driver. The above code is a snippet from the program based on the Qt Widget Application.