c++ - QT SQL notification with payload -
i'm working on applciation add table view new row, inserted db's table. started basic class handle notifies , setted triggers:
create or replace function notify_tableiwanttoobserve_update() returns trigger $$ declare begin perform pg_notify( cast('tableiwanttoobserve_update' text), (new.tableiwanttoobserve_id)::text); return new; end; $$ language plpgsql; create trigger trigger_notify_tableiwanttoobserve_update after update on tableiwanttoobserve each row execute procedure notify_tableiwanttoobserve_update();
so jsut send notfy id of updated row in payload. want - becous reloading whole table won't trick later.
i checked documentaton of qsqldriver http://doc.qt.io/qt-5/qsqldriver.html#notification-1
with it, created "handler":
// that's constructor
mydb = new qsqldatabase(qsqldatabase::adddatabase("qpsql", "main")); //removed data here (just fro sake of post) mydb->sethostname("-"); mydb->setport(0); mydb->setdatabasename("-"); mydb->setusername("-"); mydb->setpassword("-"); mydb->open(); if( mydb->isopen() ) { qdebug()<<"connected db!"; qobject::connect( mydb->driver(), signal(notification(const qstring&, qsqldriver::notificationsource, const qvariant)), this, slot(slot_dbnotification_recieved_notifiandpayload((const qstring&, const qvariant))); ); } else qdebug()<<"not connected db!";
but jsut wont work. driver's signal useing single qstring connect - version needed (with additional info) wont connect.
i updated qt 5.7, still in qtcreater, shows me driver's signal single string.
is there fix that? realy need use signal retrieve updated row id.
edit 1:
that slot of handler:
void notifihandlerr::slot_dbnotification_recieved_notifiandpayload(const qstring& msg, const qvariant &payload) { qdebug() << "i notified : " + msg+" data : "+payload.tostring(); }
edit 2:
i tried add qsqldriver::notificationsource argument in slot, couldn't - still repeated error in .h notificationsource wasn't declared.
edit 3:
i'm adding here of code (handler class)
// whole .h #include <qdebug> #include <qobject> #include <qstring> #include <qsqldatabase> #include <qsqldriver> #include <qvariant> #include <qsqldriverplugin> #include <qsqldriver.h> class handler : public qobject { q_object public slots: void slot_dbnotification_recieved_notifiandpayload (const qstring& name, qsqldriver::notificationsource source, const qvariant& payload); public: explicit handler(); ~handler(); private: qsqldatabase mydb; }; //whole .cpp #include "handler.h" handler::handler() { mydb = new qsqldatabase(qsqldatabase::adddatabase("qpsql", "main")); mydb->sethostname("-"); mydb->setport(0); mydb->setdatabasename("-"); mydb->setusername("-"); mydb->setpassword("-"); mydb->open(); if( mydb->isopen() ) { qdebug()<<"connected db!"; mydb->driver()->subscribetonotification("tableiwanttoobserve_update"); qobject::connect( mydb->driver(), signal(notification(const qstring&, qsqldriver::notificationsource, const qvariant)), this, slot(slot_dbnotification_recieved_notifiandpayload((const qstring&, const qvariant))); ); } else qdebug()<<"not connected db!"; } handler::~handler() { mydb->driver()->unsubscribefromnotification("tableiwanttoobserve_update"); mydb->cloe(); } void notificationmaster::slot_dbnotification_recieved_notifiandpayload (const qstring &name, qsqldriver::notificationsource source, const qvariant &payload) { qdebug() << "i notified : " + name+" data : "+payload.tostring(); }
and eliminate idea - added
qt += sql
in .pro file
your slot has wrong signature, here how should define it.
in header file:
//in order able use enum qsqldriver::notificationsource #include <qsqldriver> ... ... class handler : public qobject{ q_object public: explicit handler(qobject *parent = 0); ~handler(); ... ... ... public slots: void sqlnotification(const qstring& name, qsqldriver::notificationsource source, const qvariant& payload); ... ... };
and in constructor, when connecting slot, should subscribe notification first:
qsqldatabase::database().driver()->subscribetonotification("notification_name"); connect(qsqldatabase::database().driver(), signal(notification(qstring,qsqldriver::notificationsource,qvariant)), this, slot(sqlnotification(qstring,qsqldriver::notificationsource,qvariant)));
you may need unsubscribe in destructor(since don't want receive notification more):
qsqldatabase::database().driver()->unsubscribefromnotification("notification_name");
and slot implementation:
void handler::sqlnotification(const qstring &name, qsqldriver::notificationsource source, const qvariant &payload){ switch(source){ case qsqldriver::unknownsource: qdebug() << "unkown source, name: " << name << "payload:" << payload.tostring(); break; case qsqldriver::selfsource: qdebug() << "self source, name: " << name << "payload:" << payload.tostring(); break; case qsqldriver::othersource: qdebug() << "other source, name: " << name << "payload:" << payload.tostring(); break; } }
Comments
Post a Comment