关键点:ui界面函数包含了线程类,线程里包含了网络类。
主函数
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
ui界面函数
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->pushButton_3->setEnabled(false);
ui->pushButton_4->setEnabled(false);
ui->pushButton_5->setEnabled(false);
ui->pushButton_6->setEnabled(false);
ui->pushButton_8->setEnabled(true);
ui->pushButton_9->setEnabled(false);
ui->lineEdit->setText("127.0.0.1");
ui->lineEdit_2->setText("192.168.10.200");
ui->lineEdit_3->setText("255.255.255.0");
ui->lineEdit_4->setText("192.168.10.1");
newThread = new QThread;
myObject = new myThread;
myObject->moveToThread(newThread); //创建线程
// connect(ui->pushButton,SIGNAL(clicked()),myObject,SLOT(display()));
connect(this,SIGNAL(fuck(QString)),myObject,SLOT(lianjie(QString)));
connect(this,SIGNAL(dk()),myObject,SLOT(duan()));
connect(this,SIGNAL(sendxx(QByteArray )),myObject,SLOT(getxx(QByteArray )));
connect(myObject,SIGNAL(sendstate(int)),this,SLOT(recivecounter(int)));
connect(myObject->clientjson,SIGNAL(sendData(QByteArray)),this,SLOT(prodata(QByteArray)));
connect(myObject->clientjson,SIGNAL(sendData1(QByteArray)),this,SLOT(prodata1(QByteArray)));
newThread->start();
// myObject->setFlag(false);
// emit fuck();
}
MainWindow::~MainWindow()
{
delete ui;
}
多线程定义,关键点,这个网络类包含在线程类里。
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include "nfclientjson.h"
class myThread : public QObject
{
Q_OBJECT
public:
explicit myThread(QObject *parent = 0);
bool stopFlag = false;
int counter = 0;
nfClientjson* clientjson = new nfClientjson();
signals:
void sendstate(int a);
public slots:
void setFlag(bool flag = false);
void lianjie(QString);
void duan();
void getxx(QByteArray);
};
#endif // MYTHREAD_H