0


QT 创建线程的三种方法

方式一:派生于QThread

  1. 派生于QThread,这是Qt创建线程中最常用的方法,重写void QThread::run(),在run写具体的内容,外部通过start调用,即可执行线程体run();

注意:

派生于QThread的类,构造函数属于主线程,run函数属于子线程,可以通过打印线程id判断。

方式二:派生于QRunable

派生于QRunable,重写run()方法里处理其他任务,调用时需要借助线程池。

  1. mythread* pth = new Mythread();
  2. QThreadPool::globalinstance()->start(pth);

注意:

  1. 这种创建线程的方法的最大缺点就是:不能使用QT的信号-槽机制,因为QRunnable不会继承QObject。但是这种方法的好处就是,可以让QThreadPool来管理线程,QThreadPool会自动的清理我们新建的QRunnable对象

方式三:moveToThread

派生于QObject,使用moveToThread方法,将QThread对象作为私有成员,在构造函数里moveToThread,然后启动线程。

  1. this->moveToThread(&m_th);
  2. m_th.start();

方式一的实现:

  1. thread01.h
  1. #ifndef THREAD01_H
  2. #define THREAD01_H
  3. #include <QThread>
  4. class Thread01 : public QThread
  5. {
  6. Q_OBJECT;
  7. public:
  8. Thread01();
  9. void run() override;
  10. };
  11. #endif // THREAD01_H
  1. thread01.cpp
  1. #include "thread01.h"
  2. #include <QDebug>
  3. #include <iostream>
  4. using namespace std;
  5. Thread01::Thread01()
  6. {
  7. cout<< "Thread01 construct fun" << QThread::currentThread() << endl;
  8. }
  9. void Thread01::run()
  10. {
  11. cout<< "Thread01::run" << QThread::currentThread() << endl;
  12. int index = 0;
  13. while(1)
  14. {
  15. qDebug() << index++;
  16. QThread::msleep(500);
  17. }
  18. }

main.cpp

  1. #include <QCoreApplication>
  2. #include "thread01.h"
  3. #include <iostream>
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication a(argc, argv);
  8. cout<< "main thread" << QThread::currentThread() << endl;
  9. Thread01 th;
  10. th.start();
  11. cout<< "main thread end" << endl;
  12. return a.exec();
  13. }

运行:

方式二的实现:

  1. thread02.h
  1. #ifndef THREAD02_H
  2. #define THREAD02_H
  3. #include <QRunnable>
  4. class Thread02 : public QRunnable
  5. {
  6. public:
  7. Thread02();
  8. ~Thread02();
  9. void run() override;
  10. };
  11. #endif // THREAD02_H

thread02.cpp

  1. #include "thread02.h"
  2. #include <QThread>
  3. #include <iostream>
  4. using namespace std;
  5. Thread02::Thread02()
  6. {
  7. cout<< "Thread01 construct fun" << QThread::currentThread() << endl;
  8. }
  9. Thread02::~Thread02()
  10. {
  11. cout<< "Thread01 destructor fun" << QThread::currentThread() << endl;
  12. }
  13. void Thread02::run()
  14. {
  15. cout<< "Thread02 construct fun" << QThread::currentThread() << endl;
  16. }

main.cpp

  1. #include <QCoreApplication>
  2. #include "thread01.h"
  3. #include "thread02.h"
  4. #include <iostream>
  5. #include <QThreadPool>
  6. using namespace std;
  7. int main(int argc, char *argv[])
  8. {
  9. QCoreApplication a(argc, argv);
  10. cout<< "main thread" << QThread::currentThread() << endl;
  11. /* Thread01 th;
  12. th.start();*/
  13. Thread02 *th = new Thread02();
  14. QThreadPool::globalInstance()->start(th);
  15. cout<< "main thread end" << endl;
  16. return a.exec();
  17. }

运行:

方式三的实现

thread03.h

  1. #ifndef THREAD03_H
  2. #define THREAD03_H
  3. #include <QObject>
  4. #include <QThread>
  5. class thread03 : public QObject
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit thread03(QObject *parent = nullptr);
  10. public slots:
  11. void fun();
  12. signals:
  13. private:
  14. QThread m_th;
  15. };
  16. #endif // THREAD03_H

thread03.cpp

  1. #include "thread03.h"
  2. #include <QDebug>
  3. #include <iostream>
  4. using namespace std;
  5. thread03::thread03(QObject *parent)
  6. : QObject{parent}
  7. {
  8. this->moveToThread(&m_th);
  9. m_th.start();
  10. cout<< "Thread03 construct fun" << QThread::currentThread() << endl;
  11. }
  12. void thread03::fun()
  13. {
  14. cout<< "Thread03 fun" << QThread::currentThread() << endl;
  15. qDebug() << "func";
  16. int index = 0;
  17. while(1)
  18. {
  19. qDebug() << index++;
  20. QThread::msleep(300);
  21. }
  22. }

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include "thread03.h"
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class Widget; }
  7. QT_END_NAMESPACE
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. Widget(QWidget *parent = nullptr);
  13. ~Widget();
  14. private slots:
  15. void on_pushButton_clicked();
  16. signals:
  17. void sig_fun();
  18. private:
  19. Ui::Widget *ui;
  20. thread03* m_pTh03 = nullptr;
  21. };
  22. #endif // WIDGET_H

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <iostream>
  4. using namespace std;
  5. Widget::Widget(QWidget *parent)
  6. : QWidget(parent)
  7. , ui(new Ui::Widget)
  8. {
  9. ui->setupUi(this);
  10. m_pTh03 = new thread03();
  11. connect(this,&Widget::sig_fun,m_pTh03,&thread03::fun);
  12. }
  13. Widget::~Widget()
  14. {
  15. delete ui;
  16. }
  17. void Widget::on_pushButton_clicked()
  18. {
  19. cout<< "on_pushButton_clicked" << QThread::currentThread() << endl;
  20. emit sig_fun();
  21. }

界面和运行结果:

标签: qt 开发语言 ui

本文转载自: https://blog.csdn.net/wanglei_11/article/details/128439004
版权归原作者 水火汪 所有, 如有侵权,请联系我们删除。

“QT 创建线程的三种方法”的评论:

还没有评论