当前位置:网站首页>[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
边栏推荐
- 14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
- Favorite transaction code management tool in SAP GUI
- 447 Bili Bili noodles warp 1
- map容器
- Fiori applications are shared through the enhancement of adaptation project
- 104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
- GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
- Deep learning -- data operation
- How to write a performance test plan
- QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
猜你喜欢
随机推荐
SAP GUI 里的收藏夹事务码管理工具
Learn MySQL from scratch - database and data table operations
Intelligent computing architecture design of Internet
Easyexcel complex data export
配置筛选机
rxjs Observable of 操作符的单步调试分析
Ida dynamic debugging apk
园区全光技术选型-中篇
2020-ViT ICLR
redis配置文件中常用配置详解[通俗易懂]
General use of qstringlist
Delete AWS bound credit card account
聊一聊Zabbix都监控哪些参数
mixconv代码
Single step debugging analysis of rxjs observable of operator
447-哔哩哔哩面经1
Pytorch sharpening chapter | argmax and argmin functions
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
leetcode - 287. 寻找重复数
LC669. 修剪二叉搜索树



![[untitled]](/img/60/9a56e8b00c386779be13308515b24f.png)





