PDA

View Full Version : Help with Listening for notifications



Akiva
31st December 2013, 18:20
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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;


//Lobby Chat
private slots: void on_actionConnect_triggered();
private slots: void connect_to_lobby(QString s);


};

#endif // MAINWINDOW_H


database.h

#ifndef DATABASE_H
#define DATABASE_H

#include <QObject>
#include <QDebug>
#include <QtSql>
#include "mainwindow.h"

class database : public QObject
{
Q_OBJECT
public:
explicit database(QObject *parent = 0);


//Lobby Chat
public: void connect_to_lobby(MainWindow *main);
private: QSqlDatabase db;
signals: void send_chat(QString);
private slots: void listen_for_notifications(QString);
private: MainWindow *mainprivate;


};

#endif // DATABASE_H


database.cpp

#include "database.h"

database::database(QObject *parent) :
QObject(parent)
{

}

void database::connect_to_lobby(MainWindow *main)
{
mainprivate = main;

qDebug() << "Adding Database Driver...";
db = QSqlDatabase::addDatabase("QPSQL");
qDebug() << "Setting Connection...";
db.setHostName("135.0.189.14");
qDebug() << "Setting Database...";
db.setDatabaseName("dominionlinux");
qDebug() << "Setting Username...";
db.setUserName("player");
qDebug() << "Setting Password...";
db.setPassword("player");

qDebug() << "Attempting to open Database";
bool ok = db.open();
if (ok == false){
qDebug() << "Failed to connect to Lobby " << db.lastError();
}
else
{
qDebug() << "Success; Connected to Database as 'player'!";
QSqlQuery query;
// Select all the entries from the table "lobbychat", but only 25
qDebug() << "Preparing Query...";
query.prepare("SELECT * FROM lobbychat ORDER BY time asc LIMIT 25;");
qDebug() << "Executing Query...";
query.exec();

//Set the initial lobby chat, getting history as far back as 25 lines
qDebug() << "Grabbing lobby chat text...";
connect(this,SIGNAL(send_chat(QString)),main,SLOT( connect_to_lobby(QString)));
while (query.next())
{
QString name = query.value(0).toString();
QString time = query.value(1).toString();
QString chat = query.value(2).toString();
QString s = QString("<font color='green'>" + name + "</font> <small>" + time + "</small> " + chat);
emit send_chat(s);
}
qDebug() << "Success; The 25 most recent lines of chat have been grabbed and placed into chat";

qDebug() << "Listening for chat from the database...";
db.driver()->subscribeToNotification("lobbychat");
qDebug() << "Attempting to listen for notifications...";
connect(db.driver(),SIGNAL(notification(QString)), this, SLOT(listen_for_notifications(QString)));
qDebug() << "unsure if it worked";

}
}

void database::listen_for_notifications(QString snake)
{
qDebug() << "Attempting to connect notifications to text browser";
connect(this,SIGNAL(send_chat(QString)),mainprivat e,SLOT(connect_to_lobby(QString)));
qDebug() << "Attempting to emit notification";
emit send_chat(snake);
}


mainwindow.cpp

#include "mainwindow.h"
#include "database.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionConnect_triggered()
{
database db;
db.connect_to_lobby(this);
}
void MainWindow::connect_to_lobby(QString s)
{
ui->textBrowser->append(s);
}


Thanks ahead of time.

ChrisW67
31st December 2013, 20:47
You connect to the notification signal just like any other (Signals and slots),


connect(db.driver(), SIGNAL(notification(QString)), SLOT(myNotificationHandler(QString)));

BTW Googling found an example in this forum
http://www.qtcentre.org/threads/10727-QSqlDriver-handle-and-subscribeToNotification
And a general article on the topic
http://blog.qt.digia.com/blog/2007/11/02/asynchronous-database-event-notifications/

Akiva
31st December 2013, 21:26
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

Lesiok
1st January 2014, 11:51
First of all definition of QSqlDriver::notification signal is
void notification( const QString & name )
not
void notification( QString name ).

anda_skoa
1st January 2014, 20:01
First of all definition of QSqlDriver::notification signal is
void notification( const QString & name )
not
void notification( QString name ).

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


connect(db.driver(), SIGNAL(notification(QString)), this, SIGNAL(send_chat(QString)));


Cheers,
_