Unsolved Using Qthreads in network programming
-
Hi everyone
I am working on a project, where I have 3 pushbuttons. when the user pushes them, some informations(input number, Ip, port and pm) is read from an ini file, then the InputDetected() signal declared in my class emits and after emitting this signal, I want to make a connection with the server and send the pms to the server.
To this end, I created the buttons in the dialog class . Inside the tcpclient class, I have ConfigClass() function to read the ini file and in order to make a connection with the server and send pms to it, I Have used the InputDetected() signal which its slot is Callrun() function. Inside the Callrun function, I have started the thread() that its run function exists inside the "Mythread" class.
When I run the program, I got the error: 'tcpclient' doesn't name a type (this error is related to the mythread.h file)I would appreciate if someone could help me fix this error . Also, I don't know if I have correctly used qthread class in order to make a connection with the server. I would be grateful if someone told me if something is wrong with the concept of my code
my code:
dialog.h:#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include "tcpclient.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); tcpclient myClients[3]; private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); private: Ui::Dialog *ui; }; #endif // DIALOG_H
mythread.h:
#ifndef MYTHREAD_H #define MYTHREAD_H #include <QObject> #include <QThread> #include <QTcpSocket> #include "tcpclient.h" class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QObject *parent = nullptr); void run(); QTcpSocket *socket; tcpclient *client; signals: public slots: }; #endif // MYTHREAD_H
tcpclient.h:
#ifndef TCPCLIENT_H #define TCPCLIENT_H #include <QObject> #include <QDebug> #include <QSettings> #include <QTcpSocket> #include "mythread.h" class tcpclient : public QObject { Q_OBJECT public: explicit tcpclient(QObject *parent = nullptr); void ConfigClass(); void StartClass(int _input); QString ip; QString pm; int port; int input; QTcpSocket *socket; MyThread *mthread; signals: void InputDetected(); public slots: void Callrun(); }; #endif // TCPCLIENT_H
dialog.cpp:
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); for(int i=0; i<3; i++){ myClients[i].StartClass(i+1); } } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { emit myClients[0].InputDetected(); } void Dialog::on_pushButton_2_clicked() { emit myClients[1].InputDetected(); } void Dialog::on_pushButton_3_clicked() { emit myClients[2].InputDetected(); }
mythread.cpp:
#include "mythread.h" #include "tcpclient.h" #include <QDebug> MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() { qDebug() << "class number = " << client->input; qDebug() << "server ip is: "<<client->input; qDebug() << "server port: "<<client->input; socket = new QTcpSocket(this); socket->connectToHost(client->input,client->input); if(socket->waitForConnected(3000)) { qDebug() << "Connected!"; QByteArray data = client->input.toUtf8(); socket->write(data); socket->waitForBytesWritten(1000); socket->waitForReadyRead(3000); qDebug() << "Reading"<<socket->bytesAvailable(); qDebug() << socket->readAll(); socket->close(); } else { qDebug() << "Not connected"; } }
tcpclient.cpp:
#include "tcpclient.h" #include "mythread.h" tcpclient::tcpclient(QObject *parent) : QObject(parent) { } void tcpclient::StartClass(int _input) { input = _input; ConfigClass(); connect(this,SIGNAL(InputDetected()), this, SLOT(Callrun())); } void tcpclient::ConfigClass() { QSettings setting(QString("config.ini"),QSettings::IniFormat); QString tmp = "ServerInfo" + QString::number(input); QString m1 = tmp + "/Ip"; QString m2 = tmp + "/Port"; QString m3 = tmp + "/in"; QString m4 = tmp + "/pm"; ip = setting.value(m1,"192.167.1.23").toString(); port = setting.value(m2,0).toInt(); input = setting.value(m3,0).toInt(); pm = setting.value(m4,"hi").toString(); } void tcpclient::Callrun() { mthread = new MyThread(this); mthread->start(); }
-
@nanor
first thing i noticed:tcpclient myClients[3];
since your tcpclient` class inherits QObject you can't reference it as a value, due to the hidden copy constructor.You rather want something an array of
tcpclient*
(pointers). -
@raven-worx Thank you for replying. If I understood correctly, I have to write tcpclient myClients[3]; in mythread.h file instead of tcpclient *client;.
Now my problem is that how can I use this array inside the run function, in order to use input, port and pm? -
@nanor
no i meant change it totcpclient* myClients[3]
or even better any other Qt container for more convenient handling. -
@raven-worx I tried it, but still I am getting the same error.
-
@raven-worx said in Using Qthreads in network programming:
tcpclient myClients[3];
since your tcpclient` class inherits QObject you can't reference it as a value, due to the hidden copy constructor.I'm not following you on this one? Yes, I do know about
QObject
copying. But I don't see anywhere this is being copied, or where "reference it as a value" is happening?@nanor
You sure you want threads here? Why? Half the time people seem to want threads turns out they don't need them, and threads are hard to get right.When I run the program, I got the error: 'tcpclient' doesn't name a type (this error is related to the mythread.h file)
You get that as a runtime error? Then I guess it comes from using the
SIGNAL
/SLOT()
macros? As a suggestion it would be better if you always used the new-style connection syntax, you can get compile-time errors where the old style only errored at runtime. -
@JonB Hi. Yes I have done this project without using QThread before and now I was asked to do this by using QThread class. (I am doing internship at a company. For understanding threads I have to do the same project by using QThread class)
-
@JonB I fixed that error. This was because I have included "tcpclient.h" in mythread.h file and also included "mythread.h" inside tcpclient.h file. I deleted the part #include "tcpclient.h" from mythread file and used forwared declaration (I wrote "class tcpclient;" before class MyThread : public QThread) . My code runs, but when I push a button, It crashes!
I ran the code in debug mode, and the output is this: -
Maybe you should try to initialize your variable before accessing it?