- //MyDlg.h
- #ifndef MYDLG_H
- #define MYDLG_H
- #include <QDialog>
- class QLabel;
- class QLineEdit;
- class QComboBox;
- class QProgressBar;
- class QPushButton;
- class QGridLayout;
- class MyDlg : public QDialog
- {
- Q_OBJECT
- public:
- MyDlg(QWidget *parent = 0);
- private slots:
- void startProgress();
- private:
- QLabel *fileNum;
- QLineEdit *fileNumLineEdit;
- QLabel *progressType;
- QComboBox *comboBox;
- QProgressBar *progressBar;
- QPushButton *startBtn;
- QGridLayout *mainLayout;
- };
- #endif // MYDLG_H
- //MyDlg.cpp
- #include <QDialog>
- #include <QLabel>
- #include <QLineEdit>
- #include <QComboBox>
- #include <QPushButton>
- #include <QProgressBar>
- #include <QProgressDialog>
- #include <QWidget>
- #include <QGridLayout>
- #include "MyDlg.h"
- MyDlg::MyDlg(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, 1, 1);
- mainLayout->addWidget(fileNumLineEdit, 0, 1, 1, 1);
- mainLayout->addWidget(progressType, 1, 0, 1, 1);
- mainLayout->addWidget(comboBox, 1, 1, 1 ,1);
- mainLayout->addWidget(progressBar, 2, 1, 1, 2);
- mainLayout->addWidget(startBtn, 3, 1, 1, 1);
- mainLayout->setMargin(15);
- mainLayout->setSpacing(10);
- connect(startBtn, SIGNAL(clicked()), this, SLOT(startProgress()));
- }
- void MyDlg::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
- {
- 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 ;
- }
- }
- }
- }
- //main.cpp
- #include <QApplication>
- #include <QTextCodec>
- #include "MyDlg.h"
- int main(int argc, char *argv[])
- {
- QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
- QApplication app(argc, argv);
- MyDlg md;
- md.show();
- return app.exec();
- }
程序运行截图: