C++ 带负载的QT-SQL通知
我目前正在开发一个应用程序,它将添加到表视图的新行中,该行被插入到db的表中。我从处理通知和设置触发器的基本类开始:C++ 带负载的QT-SQL通知,c++,mysql,qt,notifications,psql,C++,Mysql,Qt,Notifications,Psql,我目前正在开发一个应用程序,它将添加到表视图的新行中,该行被插入到db的表中。我从处理通知和设置触发器的基本类开始: CREATE OR REPLACE FUNCTION notify_tableIWantToObserve_update() RETURNS trigger AS $$ DECLARE BEGIN PERFORM pg_notify( CAST('tableIWantToObserve_update' AS text), (NEW.ta
CREATE OR REPLACE FUNCTION notify_tableIWantToObserve_update()
RETURNS trigger AS $$
DECLARE
BEGIN
PERFORM pg_notify(
CAST('tableIWantToObserve_update' AS text),
(NEW.tableIWantToObserve_id)::text);
return new;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER tRIGGER_notify_tableIWantToObserve_update
AFTER UPDATE
ON tableIWantToObserve
FOR EACH ROW
EXECUTE PROCEDURE notify_tableIWantToObserve_update();
因此,它将jsut发送有效负载中具有更新行id的notfy。这正是我想要的——因为重新装填整张桌子不会在以后起作用
我查了QSqlDriver的文档
用它,我创建了我的“处理程序”:
//这就是它的构造器
MyDB = new QSqlDatabase(QSqlDatabase::addDatabase("QPSQL", "Main"));
//Removed my data from here (just fro sake of this post)
MyDB->setHostName("-");
MyDB->setPort(0);
MyDB->setDatabaseName("-");
MyDB->setUserName("-");
MyDB->setPassword("-");
MyDB->open();
if( MyDB->isOpen() )
{
qDebug()<<"Connected to 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 to DB!";
在my.pro文件中,您的插槽有一个错误的签名,下面是您应该如何定义它 在头文件中:
//in order to be able to use the 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);
...
...
};
您可能需要在析构函数中取消订阅(因为您不想再收到通知):
以及您的插槽实现:
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;
}
}
void Handler::SqlNotification(常量QString&name,QSqlDriver::NotificationSource source,常量QVariant&payload){
开关(源){
案例QSqlDriver::未知源:
qDebug()我和anserw中发布的代码或多或少是正确的,但需要做的是在qt creator的较新版本中重新创建Whole项目,这样它就可以解决缺少函数和名称空间的问题。只需创建新项目并将所有文件粘贴到那里。Aa我提到过(@Edit 2)-我遇到了错误错误:每当我尝试使用NotificationSource时,都没有声明“QSqlDriver::NotificationSource”。我也第一次实现了我的代码liek-我仍然遇到了错误,而且看起来驱动程序只得到了一个字符串的插槽。请确保在.h
中包含正如我在回答中所写的那样。我认为这是“基本”的东西,所以我没有在这里包括这部分代码。您描述的错误发生在您使用enumNotificationSource
而不使用#include
时。请仔细检查并提供更多信息(可能您的整个源代码都在pastebin链接中)…完成了。但它看起来仍然不像是代码中的错误-它是liek库出了问题。我有QSqlServer作为一个类,但我只有一个strin信号-就像它得到了版本<5一样
QT += sql
//in order to be able to use the 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);
...
...
};
QSqlDatabase::database().driver()->subscribeToNotification("notification_name");
connect(QSqlDatabase::database().driver(),
SIGNAL(notification(QString,QSqlDriver::NotificationSource,QVariant)), this,
SLOT(SqlNotification(QString,QSqlDriver::NotificationSource,QVariant)));
QSqlDatabase::database().driver()->unsubscribeFromNotification("notification_name");
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;
}
}