Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

c++ - Error related to using Qthread class in network programming

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();
  }

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();
 }

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->ip;
   qDebug() << "server port: "<<client->port;


   socket = new QTcpSocket(this);

   socket->connectToHost(client->ip,client->port);


   if(socket->waitForConnected(3000))
   {
      qDebug() << "Connected!";


      QByteArray data = client->pm.toUtf8();
      socket->write(data);


      socket->waitForBytesWritten(1000);


      socket->waitForReadyRead(3000);


      qDebug() << "Reading"<<socket->bytesAvailable();


      qDebug() << socket->readAll();

      socket->close();
   }
  else
  {
      qDebug() << "Not connected";
   }


  }
question from:https://stackoverflow.com/questions/65848919/error-related-to-using-qthread-class-in-network-programming

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...