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

时间:2023-03-09 07:55:05
【转】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 = ): QThread(parent)
        {

        };
        void test()
        {

                start(HighestPriority);
        };
protected:
    void run()
        {
                ;
                )
                {
                        msleep();
                        i++;
                        emit notify(i);
                }

        };
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = )
    {
        resize(, );
        centralWidget = new QWidget(this);
        progressBar = new QProgressBar(centralWidget);
        progressBar->setGeometry(QRect(, , , ));
        progressBar->setValue();
        pushButton = new QPushButton("test",centralWidget);
        pushButton->setGeometry(QRect(, , , ));
        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