码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-23 12:28:13      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:size   parent   char   div   star   分享   qwidget   tool   val   

技术分享
#include <QApplication>
#include <QThread>
#include <QMainWindow>
#include <QProgressBar>
#include <QPushButton>
class RenderThread : public QThread
{
        Q_OBJECT
signals:
        void notify(int);
public:
        RenderThread(QObject *parent = 0): QThread(parent)
        {

        };
        void test()
        {

                start(HighestPriority);
        };
protected:
    void run()
        {
                int i =0;
                while (i<101)
                {
                        msleep(50);
                        i++;
                        emit notify(i);
                }

        };
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0)
    {
        resize(600, 400);
        centralWidget = new QWidget(this);
        progressBar = new QProgressBar(centralWidget);
        progressBar->setGeometry(QRect(130, 180, 321, 23));
        progressBar->setValue(0);
        pushButton = new QPushButton("test",centralWidget);
        pushButton->setGeometry(QRect(110, 100, 75, 23));
        QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(OnClicked()));
        this->setCentralWidget(centralWidget);
    };
    ~MainWindow(){};

private:
    QProgressBar *progressBar;
    QPushButton *pushButton;
    QWidget *centralWidget;
    RenderThread render;
public slots:
    void OnClicked()
    {
        connect(&render,SIGNAL(notify(int)),this,SLOT(OnNotify(int)));
        render.test();    
    };
    void OnNotify(int i)
    {
        progressBar->setValue(i);    
    };

};
#include "test.moc"
int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    MainWindow window;
    window.show();
    return app.exec();
}
技术分享

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

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

标签:size   parent   char   div   star   分享   qwidget   tool   val   

原文地址:http://www.cnblogs.com/tiandsp/p/7580577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!