今天来看看Qt如何启动一个线程吧,代码就以精通Qt4编程的为例

先来给出每个文件的相关代码然后再加以分析

 

 

Cpp代码   收藏代码
  1. //*************dialog.h**************//  
  2.   
  3.    
  4. #ifndef DIALOG_H  
  5. #define DIALOG_H  
  6. #include <QDialog>  
  7. #define MAXSIZE 5  //最大的线程数  
  8. class QDialogButtonBox;  
  9. class QProgressBar;  
  10. class QPushButton;  
  11. class WorkThread;  
  12. class ThreadDlg : public QDialog  
  13. {  
  14.     Q_OBJECT  
  15. public:  
  16.     ThreadDlg(QWidget *parent = 0);  
  17. public slots:  
  18.         void start();     
  19.         void stop();  
  20. private:  
  21.     QPushButton *startButton;  
  22.     QPushButton *quitButton;  
  23.     QPushButton *stopButton;  
  24.     QDialogButtonBox *buttonBox;  
  25.         WorkThread* threadVector[MAXSIZE];  
  26. };  
  27. #endif  
  28. //***********end end end************//  

 

 

Cpp代码   收藏代码
  1. //***********mainwindow.h************//  
  2.   
  3.    
  4. #ifndef WORKTHREAD_H  
  5. #define WORKTHREAD_H  
  6. #include <QThread>  
  7. class WorkThread : public QThread  
  8. {  
  9.  protected:  
  10.      void run();   //重新实现run()函数  
  11. };  
  12.    
  13. #endif  
  14. //***********end end end************//  

 

 

 

Cpp代码   收藏代码
  1. //***********dialog.cpp************//  
  2.   
  3.    
  4. #include <QtGui>  
  5. #include "workThread.h"  
  6. #include "dialog.h"  
  7.       
  8. ThreadDlg::ThreadDlg(QWidget *parent)       
  9.     : QDialog(parent)  
  10. {  
  11.     startButton = new QPushButton(tr("开始"));  
  12.     quitButton = new QPushButton(tr("退出"));  
  13.         stopButton = new QPushButton(tr("停止"));  
  14.         stopButton->setEnabled(false);  
  15.     buttonBox = new QDialogButtonBox;  
  16.     buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);  
  17.         buttonBox->addButton(stopButton, QDialogButtonBox::ActionRole);  
  18.     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);  
  19.     connect(startButton, SIGNAL(clicked()), this, SLOT(start()));  
  20.     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));  
  21.     connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));  
  22.     QVBoxLayout *mainLayout = new QVBoxLayout;  
  23.     mainLayout->addWidget(buttonBox);  
  24.     setLayout(mainLayout);  
  25.     setWindowTitle(tr("启动线程"));  
  26. }  
  27. void ThreadDlg::start()  
  28. {  
  29.     for(int i=0;i<MAXSIZE;i++)  
  30.     {  
  31.         threadVector[i] = new WorkThread();   //创建线程  
  32.     }  
  33.       
  34.         for(int i=0;i<MAXSIZE;i++)  
  35.     {  
  36.         threadVector[i]->start(QThread::LowestPriority);   //启动线程同时设置它的优先级,当然也可以不带,使用默认的优先级  
  37.     }  
  38.     stopButton->setEnabled(true);  
  39.     startButton->setEnabled(false);  
  40. }  
  41. void ThreadDlg::stop()  
  42. {  
  43.     for(int i=0;i<MAXSIZE;i++)  
  44.     {  
  45.         threadVector[i]->terminate();    //终止线程  
  46.         threadVector[i]->wait();    //阻塞等待  
  47.     }  
  48.     startButton->setEnabled(true);  
  49.     stopButton->setEnabled(false);  
  50. }  
  51. //***********end end end************//  

 

 

Cpp代码   收藏代码
  1. //***********mainwindow.cpp************//  
  2.   
  3.    
  4.    
  5. #include "workThread.h"  
  6. #include "dialog.h"  
  7. #include <QTextEdit>  
  8. #include <QDebug>  
  9. #include <stdio.h>  
  10.  void WorkThread::run()  
  11.  {  
  12.      while(true)  
  13.          for (int n = 0; n < 10;++n) {  
  14.                     printf("%d\n",n);  //打印输出  
  15.      }  
  16.  }  
  17.    
  18. //***********end end end************//  
  19.   
  20. //***********main.cpp************//  
  21.   
  22.    
  23. #include <QApplication>  
  24. #include <QtCore>  
  25. #include "dialog.h"  
  26. int main(int argc, char *argv[])  
  27. {  
  28.     QApplication app(argc, argv);  
  29.     QTextCodec::setCodecForTr( QTextCodec::codecForName("gb2312"));   
  30.     ThreadDlg dialog;  
  31.     dialog.show();  
  32.     return dialog.exec();  
  33. }  
  34. //***********end end end************//  

 

 

以上代码简单,没有必要做过多的讲解,但是其中的“threadVector[i]->terminate(); ”有必要讲解下,terminate()函数的调用便不会立刻终止线程,因为线程的何时终止取决于系统的调度策略,所在在之后又调用了wait()函数是线程阻塞等待直到退出或者超时。

最后加以一点就是在.pro文件中加入一行代码才能成功运行:

CONFIG+=thread

 

本文来源:http://qimo601.iteye.com/blog/1287279