Qt开启多线程,主要用到类QThread。有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run()。当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程。第二种方法是继承一个QObject类,然后利用moveToThread()函数开启一个线程槽函数,将要花费大量时间计算的代码放入该线程槽函数中。第二种方法可以参考我写的另一篇博客:https://blog.csdn.net/naibozhuan3744/article/details/81201502。

下面我总结的主要是第一种方法。(注意:只有在run()函数里面才是新的线程,所有复杂逻辑都应该在run()函数里面做。当run()函数运行完毕后,该线程生命周期结束。)

创建多线程步骤如下:

a1新建一个类MyThread,基类为QThread。

a2重写类MyThread的虚函数void run();,即新建一个函数protected void run(),然后对其进行定义。

a3在需要用到多线程的地方,实例MyThread,然后调用函数MyThread::start()后,则开启一条线程,自动运行函数run()。

a4当停止线程时,调用MyThread::wait()函数,等待线程结束,并且回收线程资源。

1.1新建一个widget工程,不要勾选ui界面。然后分别在mythread.h,mythread.cpp,widget.h,widget.cpp,main.cpp分别添加如下代码。

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H #include <QThread> class MyThread : public QThread
{
public:
MyThread();
void closeThread(); protected:
virtual void run(); private:
volatile bool isStop; //isStop是易失性变量,需要用volatile进行申明
}; #endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include <QDebug>
#include <QMutex> MyThread::MyThread()
{
isStop = false;
} void MyThread::closeThread()
{
isStop = true;
} void MyThread::run()
{
while ()
{
if(isStop)
return;
qDebug()<<tr("mythread QThread::currentThreadId()==")<<QThread::currentThreadId();
sleep();
}
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <mythread.h>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMutex> class Widget : public QWidget
{
Q_OBJECT public:
Widget(QWidget *parent = );
~Widget();
void createView(); private slots:
void openThreadBtnSlot();
void closeThreadBtnSlot();
void finishedThreadBtnSlot();
// void testBtnSlot(); private:
QVBoxLayout *mainLayout;
MyThread *thread1;
}; #endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QDebug>
#include <windows.h> Widget::Widget(QWidget *parent)
: QWidget(parent)
{
createView();
} void Widget::createView()
{
/*添加界面*/
QPushButton *openThreadBtn = new QPushButton(tr("打开线程"));
QPushButton *closeThreadBtn = new QPushButton(tr("关闭线程"));
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(openThreadBtn);
mainLayout->addWidget(closeThreadBtn);
mainLayout->addStretch();
connect(openThreadBtn,SIGNAL(clicked(bool)),this,SLOT(openThreadBtnSlot()));
connect(closeThreadBtn,SIGNAL(clicked(bool)),this,SLOT(closeThreadBtnSlot())); /*线程初始化*/
thread1 = new MyThread;
connect(thread1,SIGNAL(finished()),this,SLOT(finishedThreadBtnSlot()));
} void Widget::openThreadBtnSlot()
{
/*开启一个线程*/
thread1->start();
qDebug()<<"主线程id:"<<QThread::currentThreadId();
} void Widget::closeThreadBtnSlot()
{
/*关闭多线程*/
thread1->closeThread();
thread1->wait();
} void Widget::finishedThreadBtnSlot()
{
qDebug()<<tr("完成信号finished触发");
} Widget::~Widget()
{
}

main.cpp

#include "widget.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(,);
w.show(); return a.exec();
}

1.2程序构建和运行后,结果如下图所示:

参考链接:Qt5.9一个简单的多线程实例(类QThread)(第一种方法)

最新文章

  1. linux用户不在sudoers文件中
  2. 嵌入式Linux驱动学习之路(十八)LCD驱动
  3. Eclipse - 常用插件介绍
  4. EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(一)
  5. 修改安卓串口蓝牙app问题记录
  6. iOS NSDate、NSCalendar、NSDateComponents
  7. 文本去重之MinHash算法
  8. C#入门篇6-10:字符串操作 DateTime操作
  9. Git中的文件状态和使用
  10. 在Linux 5/6上使用UDEV SCSI规则配置ASM DISK
  11. 解读CSS的背景(background)样式
  12. kinit manual
  13. MVC 向页面传值方式总结
  14. PCA主成分分析
  15. STL中map的用法
  16. python2 与python3的变化
  17. xxxx-xx-xx的时间的加减
  18. EF CodeFirst系列(7)---FluentApi配置存储过程
  19. 转 edtools
  20. 设置gem源,解决下载慢的问题

热门文章

  1. 为什么要先装IIS后装.Net Framework?
  2. minitab 输入一串数字
  3. python之网络通信协议
  4. Spring@Autowired注解与自动装配(转发)
  5. Expo大作战(三十三)--expo sdk api之MapView(地图),MailComposer(磁力传感计),Lottie(动画)
  6. Java强引用、软引用、弱引用及虚引用深入探讨
  7. (网页)JS编程中,有时需要在一个方法返回两个个或两个以上的数据
  8. scrapy系列(一)——Python 爬虫框架 Scrapy1.2 Windows 安装教程
  9. OneAlert 携手 BearyChat(倍洽)快速构建 IT 运维 on-call 机制
  10. [20180118]tstats的问题.txt