0

我正在做一个项目,我有 3 个按钮。当用户按下它们时,会从 ini 文件中读取一些信息(输入编号、IP、端口和 pm),然后在我的类中声明的 InputDetected() 信号发出,发出此信号后,我想与服务器并将 pms 发送到服务器。为此,我在对话框类中创建了按钮。在 tcpclient 类中,我有 ConfigClass() 函数来读取 ini 文件,为了与服务器建立连接并向其发送 pms,我使用了 InputDetected() 信号,它的插槽是 Callrun() 函数。在 Callrun 函数中,我启动了 thread(),它的运行函数存在于“Mythread”类中。当我运行程序时,我得到了错误:'tcpclient' 没有命名类型(这个错误与 mythread.

如果有人可以帮助我解决此错误,我将不胜感激。另外,我不知道我是否正确使用了 qthread 类来与服务器建立连接。如果有人告诉我我的代码概念是否有问题,我将不胜感激

我的代码: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

我的线程.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

对话框.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();
 }

我的线程.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";
   }


  }
4

0 回答 0