Extensible IRC Bot Question - Pointer to QTcpSocket
Hello, this is my first post here.
Anyway, I have written an IRC Bot, which i want to be extensible (I have created the Plugin loading code, but its not implemented btw)
But my only problem now, is that I cant create a pointer to a QTcpSocket, because I want to use the QTcpSocket inside the API of my IRC Bot. I just need to know how to create a pointer to a QTcpSocket object that can be accessed by other classes.
And I know its badly coded ( I’m just coding it for fun ;) )
MainWindow.h:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QtNetwork/QTcpSocket>
- #include <QSqlDatabase>
- #include <QSqlQuery>
- #include <QInputDialog>
- #include "api/permissions.h"
- #include "api/commandparser.h"
- #include <iostream>
- #include <QMessageBox>
- using namespace std;
- namespace Ui {
- class MainWindow;
- }
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0,const QStringList arguments = QStringList("THIS TEXT IS DERP YEA"));
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- QSqlDatabase sql;
- private slots:
- void readData();
- void connectToServer();
- void disconnectFromServer();
- void sqlStart();
- };
- #endif // MAINWINDOW_H
MainWindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- // Setup required variables
- server = arguments.at(0);
- port = arguments.at(1);
- nick = arguments.at(2);
- pass = arguments.at(3);
- sqlServer = arguments.at(4);
- sqlPort = arguments.at(5);
- sqlUser = arguments.at(6);
- sqlPass = arguments.at(7);
- sqlType = arguments.at(8);
- // Create socket and database instance
- // Connect signals and slots!
- connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
- connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
- connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(sqlStart()));
- connect(ui->disconnectButton, SIGNAL(clicked()), this, SLOT(disconnectFromServer()));
- connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendRawCommand()));
- //UI Stuff
- ui->lineEdit->setEnabled(false);
- ui->disconnectButton->setEnabled(false);
- }
- void MainWindow::sqlStart()
- {
- //QString password = QInputDialog::getText(this, tr("SQL Database"),tr("Password: "), QLineEdit::Normal,"");
- sql.setPort(sqlPort.toInt());
- sql.setHostName(sqlServer);
- sql.setDatabaseName("biobot");
- sql.setUserName(sqlUser);
- sql.setPassword(sqlPass);
- if (!sql.open()) ui->textEdit->append("Couldn't connect to SQL database");
- else ui->textEdit->append("Connected to SQL database!");
- permissions::registerPermission("hello", 1);
- }
- void MainWindow::connectToServer() {
- ui->textEdit->append("Connecting...");
- ui->lineEdit->setEnabled(true);
- ui->disconnectButton->setEnabled(true);
- ui->connectButton->setEnabled(false);
- socket->write((nickCmd.toStdString().c_str()));
- socket->write("USER Biobot 8 * : Biobot C++ \r\n");
- }
- void MainWindow::readData() {
- //Biohazard!Biohazard@ie.freebnc.net PRIVMSG #Biobot hi
- if(readLine.startsWith("PING"))
- {
- ui->textEdit->append(("PONG " + readLine.split(" ")[1]));
- socket->write(("PONG " + readLine.split(" ")[1]).toStdString().c_str());
- }
- else if(readLine.startsWith(":NickServ!NickServ@")&&
- readLine.contains("You have 30 seconds to identify to your nickname before it is changed."))
- {
- socket->write(passCmd.toStdString().c_str());
- }
- else if(readLine.split(" ")[1]=="PRIVMSG")
- {
- if(command.split(" ")[0]=="@hello"&&permissions::getPermission("hello", CommandParser::getName(readLine)))
- {
- socket->write(text.toStdString().c_str());
- }
- }
- ui->textEdit->append(readLine);
- if(socket->canReadLine()) readData();
- }
- void MainWindow::disconnectFromServer() {
- socket->write("QUIT GTFO\r\n");
- socket->flush();
- socket->disconnect();
- sql.close();
- ui->lineEdit->setEnabled(false);
- ui->disconnectButton->setEnabled(false);
- ui->sendButton->setEnabled(false);
- ui->textEdit->append("Disconnected.");
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
1 reply
If you want to make that pointer accessible by other classes of your project you need to either create a function in MainWindow that returns that pointer or to pass the pointer to the classes (by constructor using a setter function) that shall use it.
This seems to be a reoccurring problem for beginners I guess. Take a look a this thread [qt-project.org] as the original poster struggles with the same problem only for a GLWindow instead of a QTcpSocket.
You must log in to post a reply. Not a member yet? Register here!
