Is there any qt signal for sql database change?

随声附和 提交于 2019-11-29 02:42:45

There is no such thing. The Qt event loop and the database are not connected in any way. You only fetch/alter/delete/insert/... data and that's it. Option 1 is the one you have to do. There are ways to use TRIGGER on the server side to launch external scripts but this would not help you very much.

QSqlDriver supports notifications which emit a signal when a specific event has occurred. To subscribe to an event just use QSqlDriver::subscribeToNotification( const QString & name ). When an event that you’re subscribing to is posted by the database the driver will emit the notification() signal and your application can take appropriate action.

db.driver()->subscribeToNotification("someEventId");

The message can be posted automatically from a trigger or a stored procedure. The message is very lightweight: nothing more than a string containing the name of the event that occurred.

You can connect the notification(const QString&)signal to your slot like:

QObject::connect(db.driver(), SIGNAL(notification(const QString&)), this, SLOT(refreshView()));

I should note that this feature is not supported by MySQL as it does not have an event posting mechanism.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!