Qt多线程操作界面---在QThread更新QProgressBar

Refer from http://blog.csdn.net/tingsking18/article/details/5096172

[cpp] view plain copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. class RenderThread : public QThread  
  7. {  
  8.         Q_OBJECT  
  9. signals:  
  10.         void notify(int);  
  11. public:  
  12.         RenderThread(QObject *parent = 0): QThread(parent)  
  13.         {  
  14.   
  15.         };  
  16.         void test()  
  17.         {  
  18.   
  19.                 start(HighestPriority);  
  20.         };  
  21. protected:  
  22.     void run()  
  23.         {  
  24.                 int i =0;  
  25.                 while (i<101)  
  26.                 {  
  27.                         msleep(50);  
  28.                         i++;  
  29.                         emit notify(i);  
  30.                 }  
  31.   
  32.         };  
  33. };  
  34.   
  35. class MainWindow : public QMainWindow  
  36. {  
  37.     Q_OBJECT  
  38.   
  39. public:  
  40.     MainWindow(QWidget *parent = 0)  
  41.     {  
  42.         resize(600, 400);  
  43.         centralWidget = new QWidget(this);  
  44.         progressBar = new QProgressBar(centralWidget);  
  45.         progressBar->setGeometry(QRect(130, 180, 321, 23));  
  46.         progressBar->setValue(0);  
  47.         pushButton = new QPushButton("test",centralWidget);  
  48.         pushButton->setGeometry(QRect(110, 100, 75, 23));  
  49.         QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(OnClicked()));  
  50.         this->setCentralWidget(centralWidget);  
  51.     };  
  52.     ~MainWindow(){};  
  53.   
  54. private:  
  55.     QProgressBar *progressBar;  
  56.     QPushButton *pushButton;  
  57.     QWidget *centralWidget;  
  58.     RenderThread render;  
  59. public slots:  
  60.     void OnClicked()  
  61.     {  
  62.         connect(&render,SIGNAL(notify(int)),this,SLOT(OnNotify(int)));  
  63.         render.test();      
  64.     };  
  65.     void OnNotify(int i)  
  66.     {  
  67.         progressBar->setValue(i);      
  68.     };  
  69.   
  70. };  
  71. #include "test.moc"  
  72. int main(int argc,char* argv[])  
  73. {  
  74.     QApplication app(argc,argv);  
  75.     MainWindow window;  
  76.     window.show();  
  77.     return app.exec();  


你可能感兴趣的:(Qt多线程操作界面---在QThread更新QProgressBar)