对于qt网络新线程方法

关键点: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值