QT5 多线程简单实现

转载于https://blog.csdn.net/u014492513/article/details/52275315

项目名称:threaddlg
   其中包含的文件:
threaddlg.pro
threaddlg.h
threaddlg.cpp
main.cpp
workthread.h
workthread.cpp
        threaddlg.ui
  导入项目的时候导入.pro文件,头文件中.h文件中存放类,变量,和方法声明。界面文件threaddlg.ui是threaddlg类的界面实现,源文件中main文件是程序的入口,默认显示threaddlg类。
main.cpp代码如下:
 
 
[cpp]  view plain  copy
  1. #include "threaddlg.h"  
  2. #include <QApplication>  
  3. int main(int argc, char *argv[])  
  4. {  
  5.     QApplication a(argc, argv);  
  6.     ThreadDlg w;  
  7.     w.show();  
  8.   
  9.     return a.exec();  
  10. }  
 
  可以直接在界面文件threaddlg.ui中拖动控件,右键该控件进入编写该控件的事件流程。这里动态加载控件,在threaddlg.cpp
 
 
  文件中加载界面显示的控件,变量都放在了threaddlg.h文件中.
threaddld.h源代码如下:
 
 
 
 
[cpp]  view plain  copy
  1. #ifndef THREADDLG_H  
  2. #define THREADDLG_H  
  3. #include <QDialog>  
  4. #include <QPushButton>  
  5. #include "workthread.h"  
  6. #define MAXSIZE 2  
  7. namespace Ui {  
  8. class ThreadDlg;  
  9. }  
  10. class ThreadDlg : public QDialog  
  11. {  
  12.     Q_OBJECT  
  13. public:  
  14.     explicit ThreadDlg(QWidget *parent = 0);  
  15.     ~ThreadDlg();  
  16. private:  
  17.     Ui::ThreadDlg *ui;  
  18.     QPushButton *startBtn;  
  19.     QPushButton *stopBtn;  
  20.     QPushButton *quitBtn;  
  21.     WorkThread *workThraed[MAXSIZE];  
  22. public slots:  
  23.    void slotStart();  
  24.     void slotStop();  
  25. };  
  26. #endif // THREADDLG_H  
源文件threaddlg.cpp中定义了变量的作用,还有方法的实现,threaddlg.cpp源代码如下:
 
 
 
 
[cpp]  view plain  copy
  1. #include "threaddlg.h"  
  2. #include "ui_threaddlg.h"  
  3. #include <QHBoxLayout>  
  4. #include <QDebug>  
  5. ThreadDlg::ThreadDlg(QWidget *parent) :  
  6.     QDialog(parent),  
  7.     ui(new Ui::ThreadDlg)  
  8. {  
  9.     ui->setupUi(this);  
  10.     setWindowTitle(tr("线程"));  
  11.     startBtn=new QPushButton(tr("开始"));  
  12.     stopBtn=new QPushButton(tr("停止"));  
  13.     quitBtn=new QPushButton(tr("退出"));  
  14.     QHBoxLayout *mainLayout=new QHBoxLayout(this);  
  15.     mainLayout->addWidget(startBtn);  
  16.     mainLayout->addWidget(stopBtn);  
  17.    mainLayout->addWidget(quitBtn);  
  18.     connect(startBtn,SIGNAL(clicked()),this,SLOT(slotStart()));  
  19.     connect(stopBtn,SIGNAL(clicked()),this,SLOT(slotStop()));  
  20.     connect(quitBtn,SIGNAL(clicked()),this,SLOT(close()));  
  21. }  
  22. void ThreadDlg::slotStart()  
  23. {  
  24.     for(int i=0;i<MAXSIZE;i++)  
  25.     {  
  26.         workThraed[i]=new WorkThread();  
  27.     }  
  28.     for(int i=0;i<MAXSIZE;i++)  
  29.     {  
  30.         workThraed[i]->start();  
  31.     }  
  32.     startBtn->setEnabled(false);  
  33.     stopBtn->setEnabled(true);  
  34. }  
  35. void ThreadDlg::slotStop()  
  36. {  
  37.     for(int i=0;i<MAXSIZE;i++)  
  38.     {  
  39.         workThraed[i]->terminate();  
  40.         workThraed[i]->wait();  
  41.    }  
  42.     startBtn->setEnabled(true);  
  43.     stopBtn->setEnabled(false);  
  44. }  
  45. ThreadDlg::~ThreadDlg()  
  46. {  
  47.     delete ui;  
  48. }  
界面显示的类已经完成了,接下来再建立一个类,这个类的作用是实现多线程的用法:
还是一样先建立一个头文件用来放多线程任务中需要的变量和方法的,头文件workthread.h的源代码如下:
 
 
 
 
[cpp]  view plain  copy
  1. #ifndef WORKTHREAD_H  
  2. #define WORKTHREAD_H  
  3. #include <QThread>  
  4. class WorkThread : public QThread  
  5. {  
  6.     Q_OBJECT  
  7.    public:  
  8.     WorkThread();  
  9.    protected:  
  10.      void run();  
  11. };  
  12. #endif // WORKTHREAD_H  
方法定义好了之后,在cpp文件中实现方法。workthred.CPP文件的源代码如下:
 
 
 
 
[cpp]  view plain  copy
  1. #include "workthread.h"  
  2. #include <QtDebug>  
  3. WorkThread::WorkThread()  
  4. {  
  5. }  
  6. void WorkThread::run()  
  7. {  
  8.     while(true)  
  9.     {  
  10.         for(int n=0;n<10;n++)  
  11.             qDebug()<<n<<n<<n<<n<<n<<n;  
  12.     }  
  13. }  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值