2016-07-06 16 views
2

私は現在、データベースのテーブルに挿入されたテーブルビューの新しい行に追加されるアプリケーションに取り組んでいます。私は通知を処理するための基本的なクラスを始めとトリガアップ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ファイル

答えて

0

コードは、多かれ少なかれ正確だが、何を実行する必要がすることは、issiuesを解決しますので、qtの作成者の新しいバージョンでwhoelプロジェクトを再作成することです関数と名前空間自体が欠落しています。新しいプロジェクトを作成し、そこにすべてのファイルを貼り付けるだけです。

0

あなたのスロットの署名が間違っていますeを定義する方法です。あなたがで解除する必要があるかもしれません

QSqlDatabase::database().driver()->subscribeToNotification("notification_name"); 
connect(QSqlDatabase::database().driver(), 
     SIGNAL(notification(QString,QSqlDriver::NotificationSource,QVariant)), this, 
     SLOT(SqlNotification(QString,QSqlDriver::NotificationSource,QVariant))); 

//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()->unsubscribeFromNotification("notification_name"); 

とあなたのスロット実装:私がとanserwに掲載

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; 
    } 
} 
+0

私が述べたAA(@Edit 2) - 私はエラーを取得しています \tエラー:私はNotificationSourceを使用しようとするたびに 'QSqlDriver :: NotificationSourceは' を宣言されていません。 私はコードを実装しました。これはまだまだ間違っています。私はまだエラーが発生していますが、ドライバはシングルストリングのスロットしか持っていないようです。 – Arker

+0

あなたの '.h'ファイルに' #include 'があることを確認してください。回答は – Mike

+0

でした。私はその基本的なことを考えていませんでした。 – Arker

関連する問題