QT 多线程

 第一种:静态函数

 1 void print()
 2 {
 3     for(int i = 0;i<5;i++)
 4         qInfo()<<"hello global print";
 5 }
 6 class MainWindow:public QWidget
 7 {
 8     Q_OBJECT
 9 public:
10     MainWindow(QWidget*parent = nullptr):QWidget(parent)
11     {
12         qInfo()<<"end";
13     }
14     static void print1()
15     {
16         for(int i = 0;i<5;i++)
17             qInfo()<<"hello static print1";
18     }
19     void print2()
20     {
21         for(int i = 0;i<5;i++)
22             qInfo()<<"hello print2";
23     }
24 };
create函数说明
 1 //QThread::create使用全局函数作为入口函数。
 2 auto* thr = QThread::create(print);
 3 thr->start();
 4 
 5 //QThread::create使用类的静态函数作为入口函数。
 6 auto* thr1 = QThread::create(&MainWindow::print1);
 7 thr1->start();
 8 
 9 //QThread::create使用类的非静态函数作为入口函数。
10 auto* thr2 = QThread::create(&MainWindow::print2,&w);
11 thr2->start();
12 
13 //QThread::create使用Lambda表达式作为入口函数。
14 auto* thr3 = QThread::create([](){qInfo()<<"hello lambda";});
15 thr3->start();

给入口函数传参

 1 void print(const QString& str)
 2 {
 3     for(int i = 0;i<5;i++)
 4     {
 5         qInfo()<<"hello global print"<<str;
 6     }
 7 }
 8  9 auto* thr = QThread::create(print,"I Like Qt");
10 thr->start();
1  auto q = std::function<void(int, double)>([&](int dir, double searchmm) {
2          goHome(dir, searchmm);
3      });
4 
5      auto thread = QThread::create(q, dir, searchmm);
6      thread->start();

第二种:继承QThread

头文件:MyThread.h

 1 #include <QThread>
 2 class MyThread : public QThread
 3 {
 4     Q_OBJECT
 5 public:
 6     explicit MyThread(QObject *parent = nullptr);
 7     ~MyThread();
 8 protected:
 9     void run() override;
10 signals:
11 
12 };

源文件:MyThread.cpp

 1 #include "mythread.h"
 2 #include<QDebug>
 3 MyThread::MyThread(QObject *parent)
 4     : QThread(parent)
 5 {}
 6 MyThread::~MyThread()
 7 {
 8     qDebug()<<   "MyThread::~MyThread()";
 9 }
10 
11 void MyThread::run()
12 {
13     int i= 0;
14     while(i<200)
15     {
16         QThread::msleep(20);
17         if(i==150)
18             break;
19         qDebug()<<"i"<<i++;
20     }
21 }

源文件:Widget.cpp

 1 #include "widget.h"
 2 #include<QDebug>
 3 #include<QThread>
 4 
 5 Widget::Widget(QWidget *parent)
 6     : QWidget(parent)
 7     , ui(new Ui::Widget)
 8 {
 9     ui->setupUi(this);
10     thr = new MyThread(this);
11     connect(thr,&QThread::started,this,[](){qDebug()<<"start";});
12     connect(thr,&QThread::finished,this,[](){qDebug()<<"finished";});
13     thr->start();
14 }
15 
16 Widget::~Widget()
17 {
18     thr->quit();
19     delete thr;
20 
21     delete ui;
22 }

 第三种:移动到线程

 1 class Worker : public QObject
 2   {
 3       Q_OBJECT
 4   public slots:
 5       void doWork(const QString &parameter) {
 6           QString result;
 7           /* ... here is the expensive or blocking operation ... */
 8           emit resultReady(result);
 9       }
10 
11   signals:
12       void resultReady(const QString &result);
13   };
14 
15   class Controller : public QObject
16   {
17       Q_OBJECT
18       QThread workerThread;
19   public:
20       Controller() {
21           Worker *worker = new Worker;
22           worker->moveToThread(&workerThread);
23           connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
24           connect(this, &Controller::operate, worker, &Worker::doWork);
25           connect(worker, &Worker::resultReady, this, &Controller::handleResults);
26           workerThread.start();
27       }
28       ~Controller() {
29           workerThread.quit();
30           workerThread.wait();
31       }
32   public slots:
33       void handleResults(const QString &);
34   signals:
35       void operate(const QString &);
36   };

 

posted @ 2024-03-11 14:53  wuyuan2011woaini  阅读(64)  评论(0编辑  收藏  举报