Results 1 to 5 of 5

Thread: Help with Listening for notifications

  1. #1
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Help with Listening for notifications

    So after working on it... I have it running without any errors... but I don't know if I am receiving any signals or not. My code has been updated~ If there are any errors, it is likely on line 56 of database.cpp. I just don't know if I am listening properly, or what.
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. private:
    18. Ui::MainWindow *ui;
    19.  
    20.  
    21. //Lobby Chat
    22. private slots: void on_actionConnect_triggered();
    23. private slots: void connect_to_lobby(QString s);
    24.  
    25.  
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    database.h
    Qt Code:
    1. #ifndef DATABASE_H
    2. #define DATABASE_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <QtSql>
    7. #include "mainwindow.h"
    8.  
    9. class database : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit database(QObject *parent = 0);
    14.  
    15.  
    16. //Lobby Chat
    17. public: void connect_to_lobby(MainWindow *main);
    18. private: QSqlDatabase db;
    19. signals: void send_chat(QString);
    20. private slots: void listen_for_notifications(QString);
    21. private: MainWindow *mainprivate;
    22.  
    23.  
    24. };
    25.  
    26. #endif // DATABASE_H
    To copy to clipboard, switch view to plain text mode 

    database.cpp
    Qt Code:
    1. #include "database.h"
    2.  
    3. database::database(QObject *parent) :
    4. QObject(parent)
    5. {
    6.  
    7. }
    8.  
    9. void database::connect_to_lobby(MainWindow *main)
    10. {
    11. mainprivate = main;
    12.  
    13. qDebug() << "Adding Database Driver...";
    14. db = QSqlDatabase::addDatabase("QPSQL");
    15. qDebug() << "Setting Connection...";
    16. db.setHostName("135.0.189.14");
    17. qDebug() << "Setting Database...";
    18. db.setDatabaseName("dominionlinux");
    19. qDebug() << "Setting Username...";
    20. db.setUserName("player");
    21. qDebug() << "Setting Password...";
    22. db.setPassword("player");
    23.  
    24. qDebug() << "Attempting to open Database";
    25. bool ok = db.open();
    26. if (ok == false){
    27. qDebug() << "Failed to connect to Lobby " << db.lastError();
    28. }
    29. else
    30. {
    31. qDebug() << "Success; Connected to Database as 'player'!";
    32. QSqlQuery query;
    33. // Select all the entries from the table "lobbychat", but only 25
    34. qDebug() << "Preparing Query...";
    35. query.prepare("SELECT * FROM lobbychat ORDER BY time asc LIMIT 25;");
    36. qDebug() << "Executing Query...";
    37. query.exec();
    38.  
    39. //Set the initial lobby chat, getting history as far back as 25 lines
    40. qDebug() << "Grabbing lobby chat text...";
    41. connect(this,SIGNAL(send_chat(QString)),main,SLOT(connect_to_lobby(QString)));
    42. while (query.next())
    43. {
    44. QString name = query.value(0).toString();
    45. QString time = query.value(1).toString();
    46. QString chat = query.value(2).toString();
    47. QString s = QString("<font color='green'>" + name + "</font> <small>" + time + "</small> " + chat);
    48. emit send_chat(s);
    49. }
    50. qDebug() << "Success; The 25 most recent lines of chat have been grabbed and placed into chat";
    51.  
    52. qDebug() << "Listening for chat from the database...";
    53. db.driver()->subscribeToNotification("lobbychat");
    54. qDebug() << "Attempting to listen for notifications...";
    55. connect(db.driver(),SIGNAL(notification(QString)), this, SLOT(listen_for_notifications(QString)));
    56. qDebug() << "unsure if it worked";
    57.  
    58. }
    59. }
    60.  
    61. void database::listen_for_notifications(QString snake)
    62. {
    63. qDebug() << "Attempting to connect notifications to text browser";
    64. connect(this,SIGNAL(send_chat(QString)),mainprivate,SLOT(connect_to_lobby(QString)));
    65. qDebug() << "Attempting to emit notification";
    66. emit send_chat(snake);
    67. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "database.h"
    3. #include "ui_mainwindow.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_actionConnect_triggered()
    18. {
    19. database db;
    20. db.connect_to_lobby(this);
    21. }
    22. void MainWindow::connect_to_lobby(QString s)
    23. {
    24. ui->textBrowser->append(s);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Thanks ahead of time.
    Last edited by Akiva; 31st December 2013 at 19:45.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help with Listening for notifications

    You connect to the notification signal just like any other (Signals and slots),
    Qt Code:
    1. connect(db.driver(), SIGNAL(notification(QString)), SLOT(myNotificationHandler(QString)));
    To copy to clipboard, switch view to plain text mode 
    BTW Googling found an example in this forum
    http://www.qtcentre.org/threads/1072...ToNotification
    And a general article on the topic
    http://blog.qt.digia.com/blog/2007/1...notifications/

  3. The following user says thank you to ChrisW67 for this useful post:

    Akiva (31st December 2013)

  4. #3
    Join Date
    Sep 2013
    Posts
    32
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Help with Listening for notifications

    HOW ?!!?!!?
    Can I repay you?

    I just don't understand why I couldnt get it to work, and yet... you, just beautifully solved all my woes, with one line of code .

    Thank you

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with Listening for notifications

    First of all definition of QSqlDriver::notification signal is
    Qt Code:
    1. void notification( const QString & name )
    To copy to clipboard, switch view to plain text mode 
    not
    Qt Code:
    1. void notification( QString name )
    To copy to clipboard, switch view to plain text mode 
    .

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with Listening for notifications

    Quote Originally Posted by Lesiok View Post
    First of all definition of QSqlDriver::notification signal is
    Qt Code:
    1. void notification( const QString & name )
    To copy to clipboard, switch view to plain text mode 
    not
    Qt Code:
    1. void notification( QString name )
    To copy to clipboard, switch view to plain text mode 
    .
    Those two are equivalent as far as the connect() statement is concerned, so the connect is OK.

    @Akiva: your database::listen_for_notifications() slot connects again and again, a connection that you already establish before in database::connect_to_lobby() (line 41 in your code snippet).
    Unless listen_for_notification does need to something more than it currently does, you can simply connect the driver's signal to your own
    Qt Code:
    1. connect(db.driver(), SIGNAL(notification(QString)), this, SIGNAL(send_chat(QString)));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 22nd April 2012, 23:20
  2. Replies: 8
    Last Post: 5th April 2011, 10:19
  3. QSharedMemory Listening?
    By ManuMies in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2010, 13:42
  4. Subscribe to ALL Notifications
    By otternase in forum Qt Programming
    Replies: 0
    Last Post: 6th June 2010, 13:27
  5. Listening for a D-Bus signal
    By fabifi in forum Qt Programming
    Replies: 1
    Last Post: 1st February 2009, 10:13

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
  •  
Qt is a trademark of The Qt Company.