原文地址:http://blog.csdn.net/a649518776/article/details/6648347
两种进度条显示:
QProgressBar和QProgressDialog
下面例子展示两种进度条:
代码:
头文件dialog.h中:
- #include <QtGui/QDialog>
- #include <QLabel>
- #include <QLineEdit>
- #include <QProgressBar>
- #include <QComboBox>
- #include <QPushButton>
- #include <QGridLayout>
- class Dialog : public QDialog
- {
- Q_OBJECT
-
- public:
- Dialog(QWidget *parent = 0);
- ~Dialog();
- private slots:
- void startProgress();
- private:
- QLabel *FileNum;
- QLineEdit *FileNumLineEdit;
- QLabel *ProgressType;
- QComboBox *comboBox;
- QProgressBar *progressBar;
- QPushButton *startBtn;
- QGridLayout *mainLayout;
- }
- #include "dialog.h"
- #include <QProgressDialog>
- #include <QFont>
-
- Dialog::Dialog(QWidget *parent)
- : QDialog(parent)
- {
- setWindowTitle(tr("Progress"));
-
- FileNum=new QLabel;
- FileNum->setText(tr("文件数目:"));
- FileNumLineEdit=new QLineEdit;
- FileNumLineEdit->setText(tr("10000"));
-
- ProgressType=new QLabel;
- ProgressType->setText(tr("显示类型:"));
- comboBox=new QComboBox;
- comboBox->addItem(tr("progressBar"));
- comboBox->addItem(tr("progressDialog"));
-
- progressBar=new QProgressBar;
-
- startBtn=new QPushButton;
- startBtn->setText(tr("开始"));
-
- mainLayout=new QGridLayout(this);
- mainLayout->addWidget(FileNum,0,0);
- mainLayout->addWidget(FileNumLineEdit,0,1);
- mainLayout->addWidget(ProgressType,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()),this,SLOT(startProgress()))
- }
- void Dialog::startProgress()
- {
- bool ok;
- int num=FileNumLineEdit->text().toInt(&ok);
- if(!ok)
- return;
- if(comboBox->currentIndex()==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);
- QFont font("ZYSong18030",12);
- progressDlg->setFont(font);
- progressDlg->setWindowModality(Qt::WindowModal);
- progressDlg->setMinimumDuration(5);
- progressDlg->setWindowTitle(tr("please wait"));
- progressDlg->setLabelText(tr("正在复制...... "));
- progressDlg->setCancelButtonText(tr("取消"));
- progressDlg->setRange(0,num);
- for(int i=1;i<=num;i++)
- {
- progressDlg->setValue(i);
- if(progressDlg->wasCanceled())
- return;
- }
- }
- }
- #include <QtGui/QApplication>
- #include "dialog.h"
- #include <QTextCodec>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
- Dialog w;
- w.show();
-
- return a.exec();
- }