当前位置:网站首页>Qthread simple test understanding
Qthread simple test understanding
2022-04-23 06:26:00 【lingedeng】
#ifndef QTHREADTEST_H
#define QTHREADTEST_H
#include <QObject>
#include <QThread>
// method 1
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
signals:
void resultReady(const QString& result);
public slots:
void slotDoWork(const QString& param);
protected:
bool event(QEvent *e) Q_DECL_OVERRIDE;
};
class Controller : public QObject {
Q_OBJECT
public:
explicit Controller(QObject *parent=0);
~Controller();
void doSomething(const QString& param);
public slots:
void slotHandleResult(const QString &result);
signals:
void operate(const QString ¶m);
private:
QThread worker_thread_;
};
// method 2
class WorkerThread : public QThread {
Q_OBJECT
public:
WorkerThread(QObject *parent = 0);
~WorkerThread();
protected:
void run() Q_DECL_OVERRIDE;
bool event(QEvent *e) Q_DECL_OVERRIDE;
signals:
void resultReady(const QString& result);
public slots:
void slotThreadFinished();
};
class Controller2 : public QObject {
Q_OBJECT
public:
Controller2(QObject *parent = 0);
void doSomething();
public slots:
void slotHandleResult(const QString &result);
};
#endif // QTHREADTEST_H
#include "qthreadtest.h"
#include <QDebug>
#include <QEvent>
Worker::Worker(QObject *parent) :
QObject(parent)
{
}
// By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread
void Worker::slotDoWork(const QString ¶m) {
QString result("done");
qDebug() << "worker thread id: " << QThread::currentThreadId() << ", then begin sleep 5 secs...";
QThread::sleep(5);
emit resultReady(result);
}
bool Worker::event(QEvent *e) {
if (e->type() == QEvent::DeferredDelete) {
qDebug() << "thread id: " << QThread::currentThreadId() << ", DeferredDelete";
}
return QObject::event(e);
}
Controller::Controller(QObject *parent) : QObject(parent) {
Worker *worker = new Worker;
worker->moveToThread(&worker_thread_);
connect(&worker_thread_, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::slotDoWork);
connect(worker, &Worker::resultReady, this, &Controller::slotHandleResult);
worker_thread_.start();
}
Controller::~Controller() {
worker_thread_.quit();
worker_thread_.wait();
}
void Controller::doSomething(const QString& param) {
emit operate(param);
}
void Controller::slotHandleResult(const QString &result) {
qDebug() << "thread id: " << QThread::currentThreadId() << ", " << result;
}
///
// WorkerThread & Controller2
/*
void QObject::deleteLater()
{
QCoreApplication::postEvent(this, new QDeferredDeleteEvent());
}
...
bool QObject::event(QEvent *e)
{
switch (e->type()) {
....
case QEvent::DeferredDelete:
qDeleteInEventHandler(this);
break;
....
}
}
...
void qDeleteInEventHandler(QObject *o)
{
delete o;
}
*/
WorkerThread::WorkerThread(QObject *parent) : QThread(parent) {
}
WorkerThread::~WorkerThread() {
qDebug() << "no event loop, thread id: " << QThread::currentThreadId() << ", ~WorkerThread";
}
void WorkerThread::run() {
QString result("done");
qDebug() << "worker thread id: " << QThread::currentThreadId() << ", then begin sleep 5 secs...";
QThread::sleep(5);
emit resultReady(result);
}
// If there is no event loop Of thread Use , that thread Destroy objects when finished
// When returning to the message loop of the main thread , In the main thread event queue Events in are used notify() Method for distribution
// notify() Distribute the event to the recipient : receiver->event(event), namely WorkerThread::event(event), Finally call to QObject::event(...)
// first event type = 43 (QEvent::MetaCall), event type = 52 (QEvent::DeferredDelete)
// QEvent::MetaCall(An asynchronous method invocation via QMetaObject::invokeMethod()), It is mainly used for signal-slot Mechanism
bool WorkerThread::event(QEvent *e) {
if (e->type() == QEvent::DeferredDelete) {
qDebug() << "no event loop, thread id: " << QThread::currentThreadId() << ", DeferredDelete";
}
bool result = QThread::event(e);
qDebug() << "no event loop, thread id: " << QThread::currentThreadId() << ", after QThread::event(...) with type = " << e->type();
return result; //QThread::event(e);
}
// Main thread execution slotThreadFinished
// call deleteLater Then the message receiver WorkerThread as well as QDeferredDeleteEvent Events are added to the main thread event queue
void WorkerThread::slotThreadFinished() {
qDebug() << "no event loop, thread id: " << QThread::currentThreadId() << ", thread finished";
deleteLater();
}
Controller2::Controller2(QObject *parent) : QObject(parent) {
}
void Controller2::doSomething() {
WorkerThread *thread = new WorkerThread;
connect(thread, &QThread::finished, thread, &WorkerThread::slotThreadFinished);
connect(thread, &WorkerThread::resultReady, this, &Controller2::slotHandleResult);
thread->start();
}
void Controller2::slotHandleResult(const QString &result) {
qDebug() << "thread id: " << QThread::currentThreadId() << ", " << result;
}
Reference resources :Qt Assistant - QThread Class
版权声明
本文为[lingedeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616199052.html
边栏推荐
- POJ - 2955 brackets interval DP
- Collections multiple parameter sorting
- 检测技术与原理
- [leetcode 54] spiral matrix
- How to grow at work
- Calculation (enter the calculation formula to get the result)
- C language file operation
- Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
- 6.Reversal
- [leetcode 67] sum of two binary numbers
猜你喜欢
St table template
In depth understanding of the relationship between dncblevel and noise denoising in the paper
lambda expressions
Linear algebra Chapter 1 - determinant
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Addition, deletion, modification and query of MySQL table
线代第四章-向量组的线性相关
Preparedstatement prevents SQL injection
Filebrowser realizes private network disk
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
随机推荐
[leetcode 59] spiral matrix II
@Problems caused by internal dead loop of postconstruct method
Troubleshooting of data deleted and reappeared problems
Fact final variable and final variable
线性代数第二章-矩阵及其运算
Three ways to create threads
MySQL best practices for creating tables
Common sense of thread pool
Optional best practices
ThreadLocal. Threadlocalmap analysis
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Detection technology and principle
MySQL basic madness theory
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
Framework analysis 1 Introduction to system architecture
[leetcode 350] intersection of two arrays II
[leetcode217] there are duplicate elements
In depth source code analysis servlet first program
Complete example demonstration of creating table to page - joint table query
IO multiplexing of 09 redis