当前位置:网站首页>QT的多线程详解(包含案例)
QT的多线程详解(包含案例)
2022-06-13 12:11:00 【房东不养猫】
#QT实现多线程
相对于现在Qt的版本,多线程的写法已经有很多种了,我下面例子采用的重写Run函数的方法实现多线程的
这个方法也是我最近做项目 总结出来的一些方法,算是分享给大家。
步骤如下:
(1).添加新的类,命名随意。右击项目文件,选择addnew,图如下:


(2)选择类 C++ class。
(3) 命名类的名称 ,选择包含QObject。
(4).打开创建的.h文件。毕竟要使用线程,所以要先包含QThread头文件,再将类的继承由QObject改为QThread,然后声明run()函数。若你想主线程给子线程传递参数,则可以采用图中的方法。图中的是构造函数的声明,实现是在CPP文件中的。
(5).打开CPP文件,包含主线程的cpp和需要采用的模块的头文件,在这个cpp中只有run函数是运行在新的线程中(PS:之前不知道这个特性,还在savethread.cpp中写入多个函数,还用定时器信号与槽 触发,结果发现,其他的不怎么运行),重要的事情说三遍!!! 若是想把耗时操作写入多线程,一定要注意代码的逻辑。
(6)到这里线程的内容就写完了,接下来就需要在主线程中启动,调用它。
打开主线程的CPP,在需要采用线程的地方调用,然后启动线程,最后关闭线程。
边栏推荐
- Notes on the development of raspberry pie (16): Raspberry pie 4b+ install MariaDB database (MySQL open source branch) and test basic operations
- 智能客服系统框架rasa
- Pulsar consumer
- 14、wpf之Border装饰器使用小记
- Analysis of different dimensions of enterprise evaluators: enterprise evaluation of Gansu Power Investment Capital Management Co., Ltd
- 004、torchserve 调用LR二分类预测
- Adaptation of multi system docking and application of packaging mode
- 高光谱真彩色图像合成原理及具体操作过程
- Idea usage
- 一文说清楚SaaS(软件即服务)
猜你喜欢
![[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)](/img/1b/92cbe7050580a0124a82f70dd3ca21.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)

Envoyer un SMS - système de carte d'accès intelligent basé sur stm32f103 + as608 module d'empreintes digitales + clé matricielle 4x4 + sim900a

9、Wide&Deep简介

2022年二建《法规》科目答案已出,请收好

想发自己的NFT,你要先搞清楚这6个问题

Based on STM32F103 - matrix key + serial port printing

【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(二)
![[truth] the reason why big factories are not afraid to spend money is...](/img/db/6d40acbb0a28e9ead029f29e0ec0b3.png)
[truth] the reason why big factories are not afraid to spend money is...

8. Deepfm introduction

浅谈常见的web攻击以及如何防范
随机推荐
Lucene从入门到实战
Camunda timer events example demo (timer events)
SaaS应用架构的最佳实践
M1 体验win11
Lucene from introduction to practice
机器学习(三)— LDA(线性判别分析)理论与代码详解
机器学习(二)—逻辑回归理论与代码详解
Based on STM32F103 - matrix key + serial port printing
Problems encountered in using the Pluto table of the flutter plug-in
MCDF Experiment 2
秒云与趋动科技联合发布容器云平台与GPU资源池化整体解决方案
Tamigou equity project sharing: transfer of 1637900 shares of Beijing Huadahua New Technology Co., Ltd
[benefits] in minutes
Lightweight real-time semantic segmentation: eNet & erfnet
致力超表面光子芯片产品研发与制造,山河光电完成数千万元Pre-A轮融资
机器学习(四)—PCA(主成分分析)理论与代码详解
Projet de développement web, développement d'une page Web
8、DeepFM介绍
003、torchserve 调用LSTM模型预测
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)