先看一种写法
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QThread>
#include<QTcpServer>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject*parent=nullptr);
~MyThread();
protected:
void run() override;
public slots:
private:
void handleNewConnection();
void handleDisConnection();
void handleReceiveData(QTcpSocket*socket);
QTcpServer*server;
};
#endif // MYTHREAD_H
#include "mythread.h"
#include<QTcpSocket>
MyThread::MyThread(QObject*parent):QThread(parent){
}
MyThread::~MyThread()
{
delete server;
}
void MyThread::run()
{
qDebug()<<"子线程id:"<<QThread::currentThreadId();
server=new QTcpServer;
connect(server,&QTcpServer::newConnection,this,&MyThread::handleNewConnection);
server->listen(QHostAddress::Any,9527);
exec();
}
void MyThread::handleNewConnection()
{
qDebug()<<"建立连接槽函数执行线程id:"<<QThread::currentThreadId();
qDebug()<<"一个客户端建立连接..."<<endl;
QTcpSocket*con=server->nextPendingConnection();
connect(con,&QTcpSocket::readyRead,this,[this,con](){
handleReceiveData(con);});
connect(con,&QTcpSocket::disconnected,this,&MyThread::handleDisConnection);
}
void MyThread::handleDisConnection()
{
qDebug()<<"断开连接槽函数执行线程id:"<<QThread::currentThreadId();
qDebug()<<"一个客户端断开连接..."<<endl;
}
void MyThread::handleReceiveData(QTcpSocket*socket)
{
qDebug()<<"接受数据槽函数执行线程id:"<<QThread::currentThreadId();
qDebug()<<"接收到数据:"<<socket->readAll()<<endl;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow),td(nullptr)
{
ui->setupUi(this);
qDebug()<<"主线程id:"<<QThread::currentThreadId();
td.start();
}
MainWindow::~MainWindow()
{
delete ui;
}
上面的运行之后处理新的连接和连接断开以及数据读取的槽函数都是在主线程中执行的
修改之后:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include<QTcpServer>
class MyThread:public QObject
{
Q_OBJECT
public:
MyThread(QObject*parent=nullptr);
~MyThread();
void run() ;
public slots:
private:
void handleNewConnection();
void handleDisConnection();
void handleReceiveData(QTcpSocket*socket);
QTcpServer*server;
};
#endif // MYTHREAD_H
#include "mythread.h"
#include<QTcpSocket>
#include<QThread>
MyThread::MyThread(QObject*parent):QObject(parent){
}
MyThread::~MyThread()
{
delete server;
}
void MyThread::run()
{
//qDebug()<<"子线程id:"<<QThread::currentThreadId();
server=new QTcpServer;
connect(server,&QTcpServer::newConnection,this,&MyThread::handleNewConnection);
server->listen(QHostAddress::Any,9527);
}
void MyThread::handleNewConnection()
{
qDebug()<<"建立连接槽函数执行线程id:"<<QThread::currentThreadId();
qDebug()<<"一个客户端建立连接..."<<endl;
QTcpSocket*con=server->nextPendingConnection();
connect(con,&QTcpSocket::readyRead,this,[this,con](){
handleReceiveData(con);});
connect(con,&QTcpSocket::disconnected,this,&MyThread::handleDisConnection);
}
void MyThread::handleDisConnection()
{
qDebug()<<"断开连接槽函数执行线程id:"<<QThread::currentThreadId();
qDebug()<<"一个客户端断开连接..."<<endl;
}
void MyThread::handleReceiveData(QTcpSocket*socket)
{
qDebug()<<"接受数据槽函数执行线程id:"<<QThread::currentThreadId();
qDebug()<<"接收到数据:"<<socket->readAll()<<endl;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"mythread.h"
#include<QThread>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QThread *sonThread;
MyThread*td;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug()<<"主线程id:"<<QThread::currentThreadId();
td=new MyThread();
sonThread=new QThread();
td->moveToThread(sonThread);
td->run();
sonThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
delete sonThread;
delete td;
}