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

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

  1. #include <QApplication>  
  2. #include <QThread>  
  3. #include <QMainWindow>  
  4. #include <QProgressBar>  
  5. #include <QPushButton>  
  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();  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值