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;
}
{
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
#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
To copy to clipboard, switch view to plain text mode
database.h
#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
#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
To copy to clipboard, switch view to plain text mode
database.cpp
#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);
}
#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)),mainprivate,SLOT(connect_to_lobby(QString)));
qDebug() << "Attempting to emit notification";
emit send_chat(snake);
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Thanks ahead of time.
Bookmarks