#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QProgressBar>
#include <QDialog>
#include <QPushButton>
class Progress : public QDialog
{
Q_OBJECT
public:
Progress(QWidget *parent = 0);
~Progress();
public slots:
void startProgress();
private:
QLabel *fileNum;
QLineEdit *fileNumEdit;
QLabel *showType;
QComboBox *comboBox;
QProgressBar *progressBar;
QPushButton *startBtn;
};
#include "widget.h"
#include <QGridLayout>
#include <QProgressDialog>
Progress::Progress(QWidget *parent)
: QDialog(parent)
{
fileNum = new QLabel(tr("文件数目:"));
fileNumEdit = new QLineEdit;
showType = new QLabel(tr("显示类型:"));
comboBox = new QComboBox;
comboBox->addItem(tr("progressBar"));
comboBox->addItem(tr("progressDialog"));
progressBar = new QProgressBar;
startBtn = new QPushButton(tr("开始"));
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addWidget(fileNum,0,0);
mainLayout->addWidget(fileNumEdit,0,1);
mainLayout->addWidget(showType,1,0);
mainLayout->addWidget(comboBox,1,1);
mainLayout->addWidget(progressBar,2,0,1,2);
mainLayout->addWidget(startBtn,3,1);
mainLayout->setMargin(15);
mainLayout->setSpacing(10);
connect(startBtn,SIGNAL(clicked(bool)),this,SLOT(startProgress()));
}
Progress::~Progress()
{
}
void Progress::startProgress()
{
//文件数目
int num = fileNumEdit->text().toInt();
if(comboBox->currentIndex() == 0)
{
//设置进度条的范围从0到文件数目
progressBar->setRange(0,num);
for(int i=1; i<=num; i++)
{
//设置进度条的值
progressBar->setValue(i);
}
}else if(comboBox->currentIndex() == 1)
{
//创建一个进度条对话框
QProgressDialog *progressDlg = new QProgressDialog(this);
//设置进度对话框出现所需等待的时间
progressDlg->setMinimumDuration(5);
progressDlg->setWindowTitle(tr("Please wait"));
progressDlg->setLabelText(tr("Copy..."));
progressDlg->setRange(0,num);
progressDlg->exec();
for(int i=1; i<=num; i++)
{
progressDlg->setValue(i);
}
}
}