私は現在、データベースのテーブルに挿入されたテーブルビューの新しい行に追加されるアプリケーションに取り組んでいます。私は通知を処理するための基本的なクラスを始めとトリガアップsetted:ペイロード付きQT SQL通知
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お送りjsutれます。それは私が欲しいものです。テーブル全体を再ロードするだけで、後でトリックをやることはありません。
私はそれでQSqlDriver http://doc.qt.io/qt-5/qsqldriver.html#notification-1
のdocumentatonを確認し、私は私の "ハンドラ" を作成しました:
//これはコンストラクタ
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!";
だが、それは文句を言わない仕事をjsut。ドライバの信号が1つのQStringを使用する場合にのみ、それが接続されます - 必要なバージョン(追加情報付き)は接続されません。
QTを5.7にアップデートしましたが、それでもQTCreaterであっても、ドライバの信号は1つの文字列でしか表示されません。
修正はありますか?私は本当にその信号を使用して、その更新された行IDを取得する必要があります。
EDIT 1:
その私のハンドラのスロット:
void NotifiHandlerr::slot_DBNotification_Recieved_NotifiAndPayload(const QString& MSG, const QVariant &payload)
{
qDebug() << "I WAS NOTIFIED ABOUT : " + MSG+" WITH DATA : "+payload.toString();
}
EDIT 2:
は、私は、私のスロットに引数としてQSqlDriver :: NotificationSourceを追加しようとしたが、私はできませんでした - NotificationSourceが宣言されていない.hのエラーを繰り返しました。
EDIT 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ファイル
私が述べたAA(@Edit 2) - 私はエラーを取得しています \tエラー:私はNotificationSourceを使用しようとするたびに 'QSqlDriver :: NotificationSourceは' を宣言されていません。 私はコードを実装しました。これはまだまだ間違っています。私はまだエラーが発生していますが、ドライバはシングルストリングのスロットしか持っていないようです。 – Arker
あなたの '.h'ファイルに' #include 'があることを確認してください。回答は –
Mike
でした。私はその基本的なことを考えていませんでした。 – Arker