QT學習記錄-進度條:

QT學習記錄-進度條:

在QT中可以用QProgressBar或着QProgressDialog來實現進度條。

 

QProgressDialog:

QProgressBar:

 

// progress.h
#ifndefPROGRESS_H
#definePROGRESS_H
#include<QtGui/QMainWindow>
#include<QPushButton>
#include<QProgressBar>
#include<QProgressDialog>
#include<QVBoxLayout>
classprogress:publicQMainWindow
{
    Q_OBJECT
public:
    progress(QWidget*parent=0);
publicslots:
    voidbarstart(void);
private:
    QPushButton*startbutton;
    QProgressBar*bar;
    QProgressDialog*process;
};
#endif//PROGRESS_H
 
// progress.cpp
#include"progress.h"
#include<windows.h>
progress::progress(QWidget*parent)
    :QMainWindow(parent)
{
    resize(500,150);
    startbutton=newQPushButton("clickme!",this);
    bar=newQProgressBar(this);
    startbutton->setGeometry(30,20,100,30);
    bar->setGeometry(30,100,300,20);
    connect(startbutton,SIGNAL(clicked()),this,SLOT(barstart()));
}
voidprogress::barstart(void)
{
#if1   //QProgressBar
       unsignedinti,j;
       bar->setRange(0,5000-1);
       for(i=0;i<5000;i++)
       {
           //for(j=0;j<5000;j++);
           //Sleep(10);
           bar->setValue(i);
       }
#else   //QProgressDialog對話框的形式
        QProgressDialogprocess(this);
        process.setLabelText(tr("processing..."));
        process.setRange(0,5000-1);
        process.setModal(true);
        process.setCancelButtonText(tr("cancel"));
        for(inti=0;i<5000;i++)
        {
            for(intj=0;j<20000;j++);
            process.setValue(i);
            if(process.wasCanceled())
                break;
        }
#endif
}
 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章