QT多线程操作
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
QT多线程操作
QT的线程开启⽅式有俩种⽅法
⽅法⼀:
1.派⽣QThread类,重写⽗类的run函数.派⽣对象调⽤strat()函数;
⼆⽅法:
1.派⽣出⼀个QObject的⼦类,再创建⼀个QThread对象;
2.把需要在线程运⾏的函数写在QObject的⼦类的槽中;
3通过QObject的⼦类的对象moveToThread()移动到线程对象中去;
4.连接connect(线程对象,SIGNAL(strated()),QObject派⽣类对象,SLOT(func()));
5.线程对象调⽤strat()函数;
官⽹推荐第⼆种⽅法开启线程,这⾥需要注意的是⼀个QThread类的派⽣对象就是⼀条线程,当线程调⽤strat()函数,默认调⽤了run()函数,run函数⼜底层调⽤了exec()事件循环;
所以线程内函数运⾏结束了,需要回收资源的话,需要先调⽤quit()函数退出事件循环,再调⽤wait()来回收线程资源;
注:QObject派⽣类对象如果槽函数是死循环需要设置⼀个flag来终⽌,对象的资源回收需要线程来完成deleteLater(),不可以设置⽗类托管内存,因为该对象已经不在主线程中了;
⽰例:
新建⼀个窗⼝类,放⼊俩个LCD控件,⼀个按钮控件;
widget.h
1 #ifndef WIDGET_H
2#define WIDGET_H
3
4 #include <QWidget>
5 #include <QThread>
6
7namespace Ui {
8class Widget;
9 }
10
11class myThread;
12class Widget : public QWidget
13 {
14 Q_OBJECT
15
16public:
17explicit Widget(QWidget *parent = nullptr);
18 ~Widget();
19
20private:
21 Ui::Widget *ui;
22 myThread* _run;
23 myThread* _run1;
24 QThread* _thread;
25 QThread* _thread1;
26 };
27
28#endif// WIDGET_H
wdiget.cpp
1 #include "widget.h"
2 #include "ui_widget.h"
3 #include "mythread.h"
4 #include <QThread>
5 #include <QDebug>
6
7
8 Widget::Widget(QWidget *parent) :
9 QWidget(parent),
10 ui(new Ui::Widget)
11 {
12 ui->setupUi(this);
13 _run = new myThread;
14 _run->_lcd1 = ui->lcdNumber;
15
16 _run1 = new myThread;
17 _run1->_lcd2 = ui->lcdNumber_2;
18
19 _thread = new QThread(this);
20 _run->moveToThread(_thread);
21
22 _thread1 = new QThread(this);
23 _run1->moveToThread(_thread1);
24
25 connect(ui->start,SIGNAL(clicked()),_run,SLOT(count_1()));
26 connect(ui->start,SIGNAL(clicked()),_run1,SLOT(count_2()));
27
28 connect(_thread,SIGNAL(finished()),_run,SLOT(deleteLater()));
29 connect(_thread1,SIGNAL(finished()),_run1,SLOT(deleteLater()));
30 _thread->start();
31 _thread1->start();
32 }
33
34 Widget::~Widget()
35 {
36 _run->_flag = false;
37 _run1->_flag = false;
38 qDebug() << __func__;
39delete ui;
40 _thread->quit();
41 _thread1->quit();
42 _thread->wait();
43 _thread1->wait();
44 }
mythread.h
1 #ifndef MYTHREAD_H
2#define MYTHREAD_H
3
4 #include <QObject>
5 #include <QLCDNumber>
6
7class myThread : public QObject
8 {
9 Q_OBJECT
10public:
11explicit myThread(QObject *parent = nullptr);
12 ~myThread();
13
14 signals:
15
16public slots:
17void count_1();
18void count_2();
19public:
20 QLCDNumber* _lcd1;
21 QLCDNumber* _lcd2;
22bool _flag = true;
23 };
24
25#endif// MYTHREAD_H
mythread.cpp
1 #include "mythread.h"
2 #include "ui_widget.h"
3 #include <QThread>
4 #include <QDebug>
5
6 myThread::myThread(QObject *parent) : QObject(parent)
7 {
8
9 }
10
11 myThread::~myThread()
12 {
13 qDebug() << __func__;
14 }
15
16void myThread::count_1()
17 {
18int i = 0;
19while (_flag)
20 {
21 i++;
22 _lcd1->display(i);
23 QThread::msleep(500);
24 qDebug() << i;
25 }
26 }
27
28void myThread::count_2()
29 {
30int i = 0;
31while (_flag)
32 {
33 i++;
34 _lcd2->display(i);
35 QThread::msleep(1000); 36//qDebug() << i;
37 }
38 }。