带有有效负载的 QT SQL 通知
QT SQL notification with payload
>我目前正在研究将插入到 db 表中的新行添加到表视图中的应用程序。我从基本类开始处理通知并设置触发器:
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();
因此,它只会发送带有有效负载中更新行 id 的 notfy。这就是我想要的 - 重新加载整个表只是以后不会起作用。
我检查了QSqlDriver的文档http://doc.qt.io/qt-5/qsqldriver.html#notification-1
有了它,我创建了我的"处理程序":
那是它的构造函数
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!";
但它就是行不通。只有使用单个QString的驾驶员信号,它才会连接它 - 我需要的版本(带有附加信息)无法连接。
我将 QT 更新到 5.7,但即使在 QTCreater 中,它也只是向我显示驱动程序的信号只有单个字符串。
有什么解决方法吗?我真的需要使用该信号来检索更新的行 ID。
编辑 1:
我的处理程序的那个插槽:
void NotifiHandlerr::slot_DBNotification_Recieved_NotifiAndPayload(const QString& MSG, const QVariant &payload)
{
qDebug() << "I WAS NOTIFIED ABOUT : " + MSG+" WITH DATA : "+payload.toString();
}
编辑2:
我试图在我的插槽中添加 QSqlDriver::NotificationSource 作为参数,但我不能 - 它仍然在 .h 中重复错误,即未声明通知源。
编辑3:
我在这里添加大部分代码(处理程序类)
// 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 to 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 to 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 WAS NOTIFIED ABOUT : " + name+" WITH DATA : "+payload.toString();
}
只是为了消除这个想法 - 我添加了
QT += sql
在我的 .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);
...
...
};
在构造函数中,当您连接插槽时,您应该首先订阅通知:
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;
}
}
我和 anserw 发布的代码或多或少是正确的,但需要做的是在较新版本的 qt creator 中重新创建 whoel 项目,以便它能够解决缺少函数和命名空间本身的 issiues。只需创建新项目并将所有文件粘贴到那里即可。
- Qt PL/SQL - 赋值运算符 - 字符串缓冲区太小
- Qt/SQL - 从 QSqlQuery exec Stored Procedure 获取列类型和名称?
- Qt SQL LIKE语句返回错误
- 如何从 Oracle 数据库中获取 qt 中 SQL 查询的传输字节大小?
- C++ Qt SQL lite 数据库连接问题
- 应用程序在 C++ QT 上执行 sql 后崩溃
- Qt Sql 无法将变量绑定到 QSqlQuery prepare 语句
- QT 5.8 SQL连接错误:Windows 10上未加载QMYSQL驱动程序
- 如何将所选行的第一列值绑定到变量并在 QT 的 SQL 命令中使用它?
- QT-写入SQL数据库时,空的qlineedits不会以null的方式传递
- QT SQL MySQL驱动程序未在运输中加载
- Qt SQL 数据库自动完成
- Qt sql 查询失败
- Qt/SQL - 从没有记录的表中获取列类型和名称
- Qt SQL:如何区分双精度和整型
- 使用代理向Qt SQL模型添加虚拟列
- Qt-Sql INSERT语句无效
- Oracle的C++Qt SQL查询长度限制
- 带有有效负载的 QT SQL 通知
- Qt SQL -配置连接到数据库