Qt5.7一个简单的多线程实例(类QThread)

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


  
  
  1. #ifndef MYTHREAD_H
  2. #define MYTHREAD_H
  3. #include <QThread>
  4. class MyThread : public QThread
  5. {
  6. public:
  7. MyThread();
  8. void closeThread();
  9. protected:
  10. virtual void run();
  11. private:
  12. volatile bool isStop; //isStop是易失性变量,需要用volatile进行申明
  13. };
  14. #endif // MYTHREAD_H

mythread.cpp


  
  
  1. #include "mythread.h"
  2. #include <QDebug>
  3. #include <QMutex>
  4. MyThread::MyThread()
  5. {
  6. isStop = false;
  7. }
  8. void MyThread::closeThread()
  9. {
  10. isStop = true;
  11. }
  12. void MyThread::run()
  13. {
  14. while ( 1)
  15. {
  16. if(isStop)
  17. return;
  18. qDebug()<<tr( "mythread QThread::currentThreadId()==")<<QThread::currentThreadId();
  19. sleep( 1);
  20. }
  21. }

widget.h


  
  
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <mythread.h>
  5. #include <QPushButton>
  6. #include <QVBoxLayout>
  7. #include <QMutex>
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. Widget(QWidget *parent = 0);
  13. ~Widget();
  14. void createView();
  15. private slots:
  16. void openThreadBtnSlot();
  17. void closeThreadBtnSlot();
  18. void finishedThreadBtnSlot();
  19. // void testBtnSlot();
  20. private:
  21. QVBoxLayout *mainLayout;
  22. MyThread *thread1;
  23. };
  24. #endif // WIDGET_H

widget.cpp


  
  
  1. #include "widget.h"
  2. #include <QDebug>
  3. #include <windows.h>
  4. Widget::Widget(QWidget *parent)
  5. : QWidget(parent)
  6. {
  7. createView();
  8. }
  9. void Widget::createView()
  10. {
  11. /*添加界面*/
  12. QPushButton *openThreadBtn = new QPushButton(tr( "打开线程"));
  13. QPushButton *closeThreadBtn = new QPushButton(tr( "关闭线程"));
  14. mainLayout = new QVBoxLayout( this);
  15. mainLayout->addWidget(openThreadBtn);
  16. mainLayout->addWidget(closeThreadBtn);
  17. mainLayout->addStretch();
  18. connect(openThreadBtn,SIGNAL(clicked( bool)), this,SLOT(openThreadBtnSlot()));
  19. connect(closeThreadBtn,SIGNAL(clicked( bool)), this,SLOT(closeThreadBtnSlot()));
  20. /*线程初始化*/
  21. thread1 = new MyThread;
  22. connect(thread1,SIGNAL(finished()), this,SLOT(finishedThreadBtnSlot()));
  23. }
  24. void Widget::openThreadBtnSlot()
  25. {
  26. /*开启一个线程*/
  27. thread1->start();
  28. qDebug()<< "主线程id:"<<QThread::currentThreadId();
  29. }
  30. void Widget::closeThreadBtnSlot()
  31. {
  32. /*关闭多线程*/
  33. thread1->closeThread();
  34. thread1->wait();
  35. }
  36. void Widget::finishedThreadBtnSlot()
  37. {
  38. qDebug()<<tr( "完成信号finished触发");
  39. }
  40. Widget::~Widget()
  41. {
  42. }

main.cpp


  
  
  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.resize( 960, 640);
  8. w.show();
  9. return a.exec();
  10. }

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

参考内容:

https://blog.csdn.net/czyt1988/article/details/64441443(正确终止线程经典教程)

https://blog.csdn.net/MyCodingLine/article/details/48597935

https://blog.csdn.net/xipiaoyouzi/article/details/8450704

https://blog.csdn.net/qq769651718/article/details/79357933(两种创建多线程方式)

https://blog.csdn.net/czyt1988/article/details/71194457

猜你喜欢

转载自blog.csdn.net/qq_28643619/article/details/93141674