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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
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
Code:
#ifndef DATABASE_H
#define DATABASE_H
#include <QObject>
#include <QDebug>
#include <QtSql>
#include "mainwindow.h"
{
Q_OBJECT
public:
explicit database
(QObject *parent
= 0);
//Lobby Chat
public: void connect_to_lobby(MainWindow *main);
private slots: void listen_for_notifications
(QString);
private: MainWindow *mainprivate;
};
#endif // DATABASE_H
database.cpp
Code:
#include "database.h"
database
::database(QObject *parent
) :{
}
void database::connect_to_lobby(MainWindow *main)
{
mainprivate = main;
qDebug() << "Adding Database Driver...";
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'!";
// 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)),mainprivate,
SLOT(connect_to_lobby
(QString)));
qDebug() << "Attempting to emit notification";
emit send_chat(snake);
}
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "database.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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.
Re: Help with Listening for notifications
You connect to the notification signal just like any other (Signals and slots),
Code:
connect(db.
driver(),
SIGNAL(notification
(QString)),
SLOT(myNotificationHandler
(QString)));
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/
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
Re: Help with Listening for notifications
First of all definition of QSqlDriver::notification signal is
Code:
void notification
( const QString & name
)
not .
Re: Help with Listening for notifications
Quote:
Originally Posted by
Lesiok
First of all definition of QSqlDriver::notification signal is
Code:
void notification
( const QString & name
)
not
.
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
Code:
connect(db.
driver(),
SIGNAL(notification
(QString)),
this,
SIGNAL(send_chat
(QString)));
Cheers,
_