Results 1 to 11 of 11

Thread: Qt doesn't detect PostgreSQL asynchronous notifications in same instance

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt doesn't detect PostgreSQL asynchronous notifications in same instance

    I have created a rule in a PosgreSQL table to execute it when inserting a row.

    It calls "NOTIFY myNotify" and works fine when I insert a row with an INSERT SQL command. Qt detects the notification well.

    When I insert a row in a table view that depends on a table model, the rule works fine, but Qt doesn't detect the notification.

    Here is a sample main function:
    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlDriver>
    3. #include "myClass.hpp"
    4.  
    5. int main(int argc, char *argv[]) {
    6. QApplication aplication(argc, argv);
    7.  
    8. // Open conection to database
    9. QSqlDatabase bd(QSqlDatabase::addDatabase("QPSQL"));
    10. bd.setHostName("localhost");
    11. bd.setDatabaseName("myDB");
    12. bd.open("postgres", "postgres");
    13.  
    14. // Load data in the table
    15. QTableView myTV;
    16. QSqlRelationalTableModel myModel(&myTV, bd);
    17. myModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    18. myModel.setTable("myTable");
    19.  
    20. // Show table with the data from database
    21. myModel.select();
    22. myTV.setModel(&myModel);
    23. myTV.show();
    24.  
    25. // Create the object with the slot to save changes to database
    26. TmyClass myObject(0, &myModel);
    27.  
    28. // Subscribe and define the treatment for the notification thrown when updating a field
    29. bd.driver()->subscribeToNotification("modified_row");
    30. QObject::connect((QObject*)bd.driver(), SIGNAL(notification(const QString&)), (QObject*)&myObject, SLOT(myNotifyHandler(const QString&)));
    31.  
    32. return aplication.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    The class to save changes manually:
    Qt Code:
    1. #ifndef TMYCLASS
    2. #define TMYCLASS
    3.  
    4. #include <QtCore/QDebug>
    5. #include <QtGui/QTableView>
    6. #include <QtSql/QSqlError>
    7. #include <QtSql/QSqlRelationalDelegate>
    8.  
    9. class TmyClass : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. TmyClass(QObject *parent, QSqlRelationalTableModel *oneModel);
    15.  
    16. public slots:
    17. void mySave(const QModelIndex &a, const QModelIndex &b);
    18. void myNotifyHandler(const QString &notification);
    19.  
    20. private:
    21. };
    22.  
    23. #endif
    24.  
    25. TmyClass::TmyClass(QObject *parent, QSqlRelationalTableModel *oneModel): QObject(parent), m_model(oneModel)
    26. {
    27. // When something changes, call the slot mySave
    28. connect(m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(mySave(const QModelIndex&, const QModelIndex&)));
    29. }
    30.  
    31.  
    32. void TmyClass::mySave(const QModelIndex &a, const QModelIndex &b)
    33. {
    34. if(!m_model->submitAll())
    35. {
    36. qDebug()<<"Error when submitting:"<<m_model->lastError().text();
    37. }
    38. }
    39.  
    40.  
    41. void TmyClass::myNotifyHandler(const QString &notification)
    42. {
    43. qDebug()<<"Detected notification from PostgreSQL: "<<notification;
    44. }
    To copy to clipboard, switch view to plain text mode 

    PostgreSQL sample database dump (is a very simple table, the important detail is "CREATE RULE "myRule" AS ON UPDATE TO "myTable" DO NOTIFY modified_row;"):
    Qt Code:
    1. SET client_encoding = 'UTF8';
    2. SET standard_conforming_strings = off;
    3. SET check_function_bodies = false;
    4. SET client_min_messages = warning;
    5. SET escape_string_warning = off;
    6. SET search_path = public, pg_catalog;
    7. SET default_tablespace = '';
    8. SET default_with_oids = false;
    9. CREATE TABLE "myTable" (
    10. "ID" integer NOT NULL,
    11. name text
    12. );
    13. ALTER TABLE public."myTable" OWNER TO postgres;
    14. CREATE SEQUENCE "myTable_ID_seq"
    15. INCREMENT BY 1
    16. NO MAXVALUE
    17. NO MINVALUE
    18. CACHE 1;
    19. ALTER TABLE public."myTable_ID_seq" OWNER TO postgres;
    20. ALTER SEQUENCE "myTable_ID_seq" OWNED BY "myTable"."ID";
    21. SELECT pg_catalog.setval('"myTable_ID_seq"', 3, true);
    22. ALTER TABLE "myTable" ALTER COLUMN "ID" SET DEFAULT nextval('"myTable_ID_seq"'::regclass);
    23. COPY "myTable" ("ID", name) FROM stdin;
    24. 1 One
    25. 2 Two
    26. 3 Three
    27. \.
    28.  
    29. ALTER TABLE ONLY "myTable" ADD CONSTRAINT pk PRIMARY KEY ("ID");
    30. CREATE RULE "myRule" AS ON UPDATE TO "myTable" DO NOTIFY modified_row;
    31. REVOKE ALL ON SCHEMA public FROM PUBLIC;
    32. REVOKE ALL ON SCHEMA public FROM postgres;
    33. GRANT ALL ON SCHEMA public TO postgres;
    34. GRANT ALL ON SCHEMA public TO PUBLIC;
    To copy to clipboard, switch view to plain text mode 

    Some links I have read:
    QSqlDriver notifications
    Blog about asynchronous database event notifications
    Asynchronous notifications in PostgreSQL
    Attached Files Attached Files
    Last edited by Auryn; 28th November 2008 at 11:40. Reason: add code
    Auryn
    Starting to learn the world of Qt

Similar Threads

  1. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.