May 4, 2012

Biohazard Biohazard
Lab Rat
1 posts

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:

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QtNetwork/QTcpSocket>
  6. #include <QSqlDatabase>
  7. #include <QSqlQuery>
  8. #include <QInputDialog>
  9. #include "api/permissions.h"
  10. #include "api/commandparser.h"
  11. #include <iostream>
  12. #include <QMessageBox>
  13.  
  14. using namespace std;
  15.  
  16. namespace Ui {
  17. class MainWindow;
  18. }
  19.  
  20. class MainWindow : public QMainWindow
  21. {
  22.     Q_OBJECT
  23.  
  24. public:
  25.     explicit MainWindow(QWidget *parent = 0,const QStringList arguments = QStringList("THIS TEXT IS DERP YEA"));
  26.     ~MainWindow();
  27.     static void SendToSocket(QString msg);
  28. private:
  29.     Ui::MainWindow *ui;
  30.     QTcpSocket * socket;
  31.     QSqlDatabase sql;
  32.     QString sqlServer,sqlPort,sqlUser,sqlPass,sqlType,server,port,nick,pass;
  33. private slots:
  34.     void readData();
  35.     void connectToServer();
  36.     void disconnectFromServer();
  37.     void sqlStart();
  38. };
  39.  
  40. #endif // MAINWINDOW_H

MainWindow.cpp
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent, const QStringList arguments) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9.     // Setup required variables
  10.     server = arguments.at(0);
  11.     port = arguments.at(1);
  12.     nick = arguments.at(2);
  13.     pass = arguments.at(3);
  14.     sqlServer = arguments.at(4);
  15.     sqlPort = arguments.at(5);
  16.     sqlUser = arguments.at(6);
  17.     sqlPass = arguments.at(7);
  18.     sqlType = arguments.at(8);
  19.     // Create socket and database instance
  20.     socket = new QTcpSocket(this);
  21.     if(sqlType=="MySQL") sql = QSqlDatabase::addDatabase("QMYSQL");
  22.     else if(sqlType=="PostgreSQL") sql = QSqlDatabase::addDatabase("QPSQL");
  23.     // Connect signals and slots!
  24.     connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
  25.     connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
  26.     connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(sqlStart()));
  27.     connect(ui->disconnectButton, SIGNAL(clicked()), this, SLOT(disconnectFromServer()));
  28.     connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendRawCommand()));
  29.     //UI Stuff
  30.     ui->lineEdit->setEnabled(false);
  31.     ui->disconnectButton->setEnabled(false);
  32. }
  33. void MainWindow::sqlStart()
  34. {
  35.     //QString password = QInputDialog::getText(this, tr("SQL Database"),tr("Password: "), QLineEdit::Normal,"");
  36.     sql.setPort(sqlPort.toInt());
  37.     sql.setHostName(sqlServer);
  38.     sql.setDatabaseName("biobot");
  39.     sql.setUserName(sqlUser);
  40.     sql.setPassword(sqlPass);
  41.     if (!sql.open()) ui->textEdit->append("Couldn't connect to SQL database");
  42.     else ui->textEdit->append("Connected to SQL database!");
  43.     permissions::registerPermission("hello", 1);
  44. }
  45.  
  46. void MainWindow::connectToServer() {
  47.     ui->textEdit->append("Connecting...");
  48.     ui->lineEdit->setEnabled(true);
  49.     ui->disconnectButton->setEnabled(true);
  50.     ui->connectButton->setEnabled(false);
  51.     socket->connectToHost(QString(server), port.toInt());
  52.     QString nickCmd = "NICK " + nick + "\r\n";
  53.     socket->write((nickCmd.toStdString().c_str()));
  54.     socket->write("USER  Biobot 8 *  : Biobot C++ \r\n");
  55. }
  56.  
  57. void MainWindow::readData() {
  58.     QString readLine = socket->readLine();
  59.     //Biohazard!Biohazard@ie.freebnc.net PRIVMSG #Biobot hi
  60.     if(readLine.startsWith("PING"))
  61.     {
  62.         ui->textEdit->append(("PONG " + readLine.split(" ")[1]));
  63.         socket->write(("PONG " + readLine.split(" ")[1]).toStdString().c_str());
  64.     }
  65.     else if(readLine.startsWith(":NickServ!NickServ@")&&
  66.             readLine.contains("You have 30 seconds to identify to your nickname before it is changed."))
  67.     {
  68.         QString passCmd = "NICKSERV identify " + pass + "\r\n";
  69.         socket->write(passCmd.toStdString().c_str());
  70.     }
  71.     else if(readLine.split(" ")[1]=="PRIVMSG")
  72.     {
  73.         QString command = CommandParser::getMessage(readLine);
  74.         if(command.split(" ")[0]=="@hello"&&permissions::getPermission("hello", CommandParser::getName(readLine)))
  75.         {
  76.             const QString text = "PRIVMSG " + CommandParser::getChannel(readLine) + ":hello world!\r\n";
  77.             socket->write(text.toStdString().c_str());
  78.         }
  79.     }
  80.     ui->textEdit->append(readLine);
  81.     if(socket->canReadLine()) readData();
  82. }
  83.  
  84. void MainWindow::disconnectFromServer() {
  85.     socket->write("QUIT GTFO\r\n");
  86.     socket->flush();
  87.     socket->disconnect();
  88.     sql.close();
  89.     ui->lineEdit->setEnabled(false);
  90.     ui->disconnectButton->setEnabled(false);
  91.     ui->sendButton->setEnabled(false);
  92.     QMessageBox::information(this, "Notice", "To reconnect, please restart the bot.");
  93.     QMessageBox::aboutQt(this, "About QT");
  94.     ui->textEdit->append("Disconnected.");
  95. }
  96.  
  97. MainWindow::~MainWindow()
  98. {
  99.     delete ui;
  100. }

1 reply

May 5, 2012

Daniel Eder Daniel Eder
Lab Rat
64 posts

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.

 
  ‹‹ Mouseover in Graphics View Framework      [Solved]across platform ››

You must log in to post a reply. Not a member yet? Register here!