The QThread class of Qt learning

QThread class

1. Introduction
The QThread sub-thread class is a control thread in the object management program. QThread begins execution in run(). By default, run() starts the event loop by calling exec(). And run the Qt event loop in the thread.
2. Implementation method 1.
The implementation method of run() of the QThread class
:
Create a new class integrating QThread, rewrite the virtual function run, and start the thread through run.
Example:

class workThread:public QThread{
    
    
	Q_OBJECT
	potected:
	void run();//虚函数run
};
void workThread::run(){
    
    ...}

2. The moveToThread()
implementation method of the QThread class:
Create a class that inherits QObject, then create a new QThread, and transfer the created class to the child thread through moveToThread(). Then start() the child thread, thus realizing a child thread. The main thread calls the method of the created class by sending a signal, so as to realize the calculation in the sub-thread.
Example:
workThread.h

class workThread:public QObject{
    
    
	Q_OBJECT
	public:
	signals:
	void sendData(const QString str);
	public slots:
	void getId(){
    
    
	qDebug() << ":now the device is" << QThread::currentThread();
	emit(setData("Hello"));}
};

contorlThread.h

class contorlThread:public QObject{
    
    
	Q_OBJECT
	public:
    contorlThread(QWidget *parent = Q_NULLPTR);
    ~contorlThread();
	signals:
	void openWork();
	slots:
	void getData(const QString str);
	private:
	workThread *work;
	QThread thread;
};

contorlThread.cpp

contorlThread::contorlThread(QWidget *parent)
    : QMainWindow(parent)
{
    
    
    ui.setupUi(this);
    work = new workThread();
    work->moveToThread(&thread);//将任务放到线程中
    connect(this, &contorlThread::openWork, work,&workThread::getId);//获取当前线程的任务id
    connect(&thread, &QThread::finished, work, &QObject::deleteLater);//线程停止发给QObject让他自己销毁
    connect(work, &workThread::sendData, this, &contorlThread::getData);//执行线程任务并将数据返回
    thread.start();//开启子线程
}
void contorlThread::getData(const QString str){
    
    
		qDebug()<<"the info has been get:"<<str;
}
contorlThread::~contorlThread(){
    
    
	thread.quit();
	thread.wait();
}

main.cpp

#include <csignal>
#include <QThread.h>
#include<qdebug.h>
int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    contorlThread w;
    w.openWork();
    return a.exec();
}

The result is:
result graph
3. Exit thread method
1. Forced stop

QThread thread;
thread.terminate();//强制终止线程
thread.wait();// 调用wait后先调用finished信号对应的槽函数,执行完成后再往下走

2. Safe stop
Stop the event loop thread started using thread (that is, exit the loop event)

QThread thread;
thread.quit();
thread.wait();// 调用wait后先调用finished信号对应的槽函数,执行完成后再往下走

Supplementary information, Qt's multi-threading technology is clearer after viewing
, it is very detailed, it is worth reading
the detailed explanation of QThread, it is very detailed, it is worth reading

Guess you like

Origin blog.csdn.net/m0_56626019/article/details/129837712