1. 程式人生 > >QT小例子GUI(主)執行緒與子執行緒之間的通訊

QT小例子GUI(主)執行緒與子執行緒之間的通訊

在主執行緒上,可以控制子執行緒啟動,停止,清零

如果子執行緒啟動的話,每一秒鐘會向主執行緒傳送一個數字,讓主執行緒更新介面上的數字。

  1. #ifndef TQT_H_
  2. #define TQT_H_
  3. #include <QtGui>
  4. #include <QtCore>
  5. class Thread : public QThread 
  6.     Q_OBJECT 
  7. private
  8.     int number; 
  9. protected
  10.     void run(); 
  11. public
  12.     Thread(QObject *parent=0); 
  13.     ~Thread(); 
  14. signals: 
  15.     void UpdateSignal(int num); 
  16.     public slots: 
  17.         void ResetSlot(); 
  18. }; 
  19. class Widget : public QWidget 
  20.     Q_OBJECT 
  21. private
  22.     QLabel *label; 
  23.     QPushButton *startButton; 
  24.     QPushButton *stopButton; 
  25.     QPushButton *resetButton; 
  26.     Thread *myThread; 
  27.     int number; 
  28. public
  29.     Widget(QWidget *parent = 0); 
  30.     ~Widget(); 
  31. signals: 
  32.     void ResetSignal(); 
  33.     public slots: 
  34.         void ClearSlot(); 
  35.         void StartSlot(); 
  36.         void StopSlot(); 
  37.         void UpdateSlot(int num); 
  38. }; 
  39. #endif
  40. //tqt.cpp
  41. #include "tqt.h"
  42. #include <assert.h>
  43. #include <windows.h>
  44. Thread::Thread(QObject *parent) 
  45.     number = 0; 
  46. Thread::~Thread() 
  47. void Thread::run() 
  48.     while(1) 
  49.     { 
  50.         emit UpdateSignal(number); 
  51.         number++; 
  52.         sleep(1); 
  53.     } 
  54. void Thread::ResetSlot() 
  55.     number = 0; 
  56.     emit UpdateSignal(number); 
  57. Widget::Widget(QWidget *parent) 
  58.     startButton = new QPushButton("start"); 
  59.     stopButton = new QPushButton("stop"); 
  60.     resetButton = new QPushButton("reset"); 
  61.     label = new QLabel("empty"); 
  62.     myThread = new Thread; 
  63.     QVBoxLayout *layout = new QVBoxLayout; 
  64.     layout->addWidget(label); 
  65.     layout->addWidget(startButton); 
  66.     layout->addWidget(stopButton); 
  67.     layout->addWidget(resetButton); 
  68.     setLayout(layout); 
  69.     connect(stopButton, SIGNAL(clicked()),  
  70.         this, SLOT(StopSlot())); 
  71.     connect(startButton, SIGNAL(clicked()), 
  72.         this, SLOT(StartSlot())); 
  73.     connect(resetButton, SIGNAL(clicked()), 
  74.         this, SLOT(ClearSlot())); 
  75.     connect(myThread, SIGNAL(UpdateSignal(int)),  
  76.         this, SLOT(UpdateSlot(int))); 
  77.     connect(this, SIGNAL(ResetSignal()), 
  78.         myThread, SLOT(ResetSlot())); 
  79.     setWindowTitle("Thread Test"); 
  80.     resize(200, 200); 
  81.     myThread->start(); 
  82. Widget::~Widget() 
  83. void Widget::StartSlot() 
  84.     myThread->start(); 
  85. void Widget::StopSlot() 
  86.     myThread->terminate(); 
  87. void Widget::UpdateSlot(int num) 
  88.     label->setText(QString::number(num)); 
  89. void Widget::ClearSlot() 
  90.     emit ResetSignal(); 
  91. //main.cpp
  92. #include <QtGui>
  93. #include "tqt.h"
  94. int main(int argc,char **argv) 
  95.     QApplication app(argc, argv); 
  96.     Widget *widget = new Widget; 
  97.     widget->show(); 
  98.     return app.exec(); 

FROM: http://blog.csdn.net/slj_win/article/details/6856428