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 
 7 namespace Ui {
 8 class Widget;
 9 }
10 
11 class myThread;
12 class Widget : public QWidget
13 {
14     Q_OBJECT
15 
16 public:
17     explicit Widget(QWidget *parent = nullptr);
18     ~Widget();
19 
20 private:
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__;
39     delete 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 
 7 class myThread : public QObject
 8 {
 9     Q_OBJECT
10 public:
11     explicit myThread(QObject *parent = nullptr);
12     ~myThread();
13 
14 signals:
15 
16 public slots:
17     void count_1();
18     void count_2();
19 public:
20     QLCDNumber* _lcd1;
21     QLCDNumber* _lcd2;
22     bool _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 
16 void myThread::count_1()
17 {
18     int i = 0;
19     while (_flag)
20     {
21         i++;
22         _lcd1->display(i);
23         QThread::msleep(500);
24         qDebug() << i;
25     }
26 }
27 
28 void myThread::count_2()
29 {
30     int i = 0;
31     while (_flag)
32     {
33         i++;
34         _lcd2->display(i);
35         QThread::msleep(1000);
36         //qDebug() << i;
37     }
38 }

猜你喜欢

转载自www.cnblogs.com/mtn007/p/12228415.html
今日推荐