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

Posted Dsp Tian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt多线程操作界面---在QThread更新QProgressBar相关的知识,希望对你有一定的参考价值。

技术分享
#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的主要内容,如果未能解决你的问题,请参考以下文章

QT多线程及通过事件进行通信(通过自定义事件,然后QApplication::postEvent给主界面,我之前用的是信号槽)

Qt多线程和GUI界面假死

Qt多线程——方式1

QT多线程操作

Qt 多线程和网络编程学习

PyQt5中多线程模块QThread解决界面卡顿无响应问题,线程池ThreadPoolExecutor解决多任务耗时操作问题