QT SQL 带有负载的通知

QT SQL notification with payload

我目前正在开发将添加到 table 查看新行的应用程序,该行已插入数据库的 table。我从基本的 class 开始处理通知并设置触发器:

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。这就是我想要的 - 因为重新加载整个 table 以后就不会成功了。

我检查了 QSqlDriver 的文档 http://doc.qt.io/qt-5/qsqldriver.html#notification-1

有了它,我创建了我的 "handler":

// 这是它的构造函数

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 中重复错误,即未声明 NotificationSource。

编辑 3:

我在此处添加大部分代码(处理程序 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 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 项目,这样它将解决缺少函数和名称空间本身的问题。只需创建新项目并将所有文件粘贴到那里。