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

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。那就是我想要的-becous重新加载整个表只是以后无法解决问题。

我检查了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中仍然重复错误,未声明NotificationSource。

编辑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;
    }
}

关于c++ - 具有有效负载的QT SQL通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38221256/

10-11 22:23
查看更多