当前位置:网站首页>[QT widget] encapsulates a simple thread management class
[QT widget] encapsulates a simple thread management class
2022-07-01 22:49:00 【Lin Qi sevenlin】
Purpose
In the use of moveToThread When creating a thread , The code that creates and destroys this part is always written repeatedly , So I want to package this part of code into a class , Used to take charge of the functions of this part , And used to manage the threads created .
Code
.h file
#ifndef LQOBJTHREADPOOL_H
#define LQOBJTHREADPOOL_H
/**
* @className LQObjThreadPool
* @brief Thread management
* @author Sevenlin
* @date 2022-06-30
* @function
* version:v1.0.0 date:2022-06-30
* 1. Thread creation and destruction
* 2. Use moveToThread To create a thread
*/
#include <QObject>
#include <QMap>
class QThread;
class LQObjThreadPool : public QObject
{
Q_OBJECT
public:
explicit LQObjThreadPool(QObject *parent = nullptr);
~LQObjThreadPool();
Q_DISABLE_COPY(LQObjThreadPool);
public:
/**
* @brief getInstance Get singleton object
* @return
*/
static LQObjThreadPool *getInstance();
/**
* @brief createThread Add thread
* @param obj QObject object
* @param isAutoDelete QObject Whether the object is released by LQObjThreadPool The object is responsible for
* @return 1 Indicates successful addition 、-1 It means that we should obj Thread created 、-2 It means that we should obj Cannot specify parent object
*/
int createThread(QObject *obj, bool isAutoDelete = true);
/**
* @brief removeThread Remove thread
* @param obj QObject object
* @return 1 Indicates that the deletion was successful 、-1 It means that we should obj Thread has not been created 、-2 It means that we should obj Cannot specify parent object
*/
int removeThread(QObject *obj);
private:
QMap<QObject *, QThread *> m_mapObjThread;
};
#endif // LQOBJTHREADPOOL_H
.cpp file
#include "lqobjthreadpool.h"
#include <QThread>
LQObjThreadPool::LQObjThreadPool(QObject *parent) : QObject(parent)
{
}
LQObjThreadPool::~LQObjThreadPool()
{
int count = m_mapObjThread.count();
if (count > 0) {
QList<QObject *> objs = m_mapObjThread.keys();
for (auto &obj : objs) {
removeThread(obj);
}
}
}
LQObjThreadPool *LQObjThreadPool::getInstance()
{
static LQObjThreadPool *threadPool = new LQObjThreadPool();
return threadPool;
}
int LQObjThreadPool::createThread(QObject *obj, bool isAutoDelete)
{
if (obj->parent()) {
return -2;
}
if (m_mapObjThread.contains(obj)) {
return -1;
}
QThread *thread = new QThread(this);
obj->moveToThread(thread);
if (isAutoDelete) {
connect(thread, &QThread::finished, obj, &QObject::deleteLater);
connect(obj, &QObject::destroyed, [&obj] {
obj = nullptr;
});
}
thread->start();
m_mapObjThread.insert(obj, thread);
return 1;
}
int LQObjThreadPool::removeThread(QObject *obj)
{
if (obj->parent()) {
return -2;
}
if (!m_mapObjThread.contains(obj)) {
return -1;
}
QThread *thread = m_mapObjThread.take(obj);
thread->quit();
thread->wait();
return 1;
}
QT in , Big guys have other good ways to use and manage threads. Please also point out
边栏推荐
- Fiori applications are shared through the enhancement of adaptation project
- [untitled]
- 内部字段分隔符
- Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant
- Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
- 恶意软件反向关闭EDR的原理、测试和反制思考
- 记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
- Deep learning -- data operation
- 【目标跟踪】|单目标跟踪指标
- Yolov5.5 call local camera
猜你喜欢

Rust language - Introduction to Xiaobai 05

“信任机器”为发展赋能

Appium automated testing foundation - Supplement: introduction to desired capabilities parameters

447-哔哩哔哩面经1

3DE 资源没东西或不对

Learn MySQL from scratch - database and data table operations

台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条

GenICam GenTL 标准 ver1.5(4)第五章 采集引擎

使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏

MySQL5.7 设置密码策略(等保三级密码改造)
随机推荐
【日常训练】66. 加一
【MySQL】索引的分类
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
聊一聊Zabbix都监控哪些参数
【JetCache】JetCache的使用方法与步骤
[jetcache] how to use jetcache
Understanding of indexes in MySQL
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
隐藏用户的创建和使用
Slope compensation
2020-ViT ICLR
元宇宙可能成为互联网发展的新方向
Object memory layout
[literacy] deep / shallow, local / global features in machine learning image processing
General use of qstringlist
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
Understanding of transactions in MySQL
切面条 C语言
人体姿态估计的热图变成坐标点的两种方案