当前位置:网站首页>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
边栏推荐
- Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
- Preparedstatement prevents SQL injection
- [leetcode 459] duplicate substring
- 图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
- [leetcode217] there are duplicate elements
- MySQL best practices for creating tables
- 8. Integer Decomposition
- Linear algebra Chapter 2 - matrices and their operations
- Generate excel template (drop-down selection, multi-level linkage)
- Customized communication between threads (reentrantlock)
猜你喜欢

Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison

C language file operation
![[leetcode 19] delete the penultimate node of the linked list](/img/ba/3c73fba8c4b4e3de7e506670144890.png)
[leetcode 19] delete the penultimate node of the linked list

SQL injection
![图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
![Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images](/img/9d/487c77b5d25d3e37fb629164c804e2.png)
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images

Mysql database foundation

MySQL table constraints and table design

Kalman filter and inertial integrated navigation

PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
随机推荐
lambda expressions
Integers have friends interval GCD + double pointer
自動控制(韓敏版)
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Numpy common function table sorting of data processing
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
JSP syntax and JSTL tag
20 excellent plug-ins recommended by idea
[leetcode 228] summary interval
[leetcode 383] ransom letter
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
How to grow at work
自动控制原理知识点整合归纳(韩敏版)
线代第四章-向量组的线性相关
RPC must know and know
String notes
MySQL table constraints and table design
Storing inherited knowledge in cloud computing
D. Optimal partition segment tree optimization DP
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations