Qt-----进度条QProgressBar

  1. //MyDlg.h  
  2. #ifndef MYDLG_H  
  3. #define MYDLG_H  
  4. #include <QDialog>  
  5. class QLabel;  
  6. class QLineEdit;  
  7. class QComboBox;  
  8. class QProgressBar;  
  9. class QPushButton;  
  10. class QGridLayout;  
  11. class MyDlg : public QDialog  
  12. {  
  13.   Q_OBJECT  
  14. public:  
  15.   MyDlg(QWidget *parent = 0);  
  16. private slots:  
  17.   void startProgress();  
  18. private:  
  19.   QLabel *fileNum;  
  20.   QLineEdit *fileNumLineEdit;  
  21.   QLabel *progressType;  
  22.   QComboBox *comboBox;  
  23.   QProgressBar *progressBar;  
  24.   QPushButton *startBtn;  
  25.   QGridLayout *mainLayout;  
  26. };  
  27. #endif // MYDLG_H  


 

  1. //MyDlg.cpp  
  2. #include <QDialog>  
  3. #include <QLabel>  
  4. #include <QLineEdit>  
  5. #include <QComboBox>  
  6. #include <QPushButton>  
  7. #include <QProgressBar>  
  8. #include <QProgressDialog>  
  9. #include <QWidget>  
  10. #include <QGridLayout>  
  11. #include "MyDlg.h"  
  12. MyDlg::MyDlg(QWidget *parent) : QDialog(parent)  
  13. {  
  14.   setWindowTitle(tr("Progress"));  
  15.   fileNum = new QLabel;  
  16.   fileNum->setText(tr("文件数目:"));  
  17.   fileNumLineEdit = new QLineEdit;  
  18.   fileNumLineEdit->setText(tr("10000"));  
  19.   progressType = new QLabel;  
  20.   progressType->setText(tr("显示类型"));  
  21.   comboBox = new QComboBox;  
  22.   comboBox->addItem(tr("progressBar"));  
  23.   comboBox->addItem(tr("progressDialog"));  
  24.   progressBar = new QProgressBar;  
  25.   startBtn = new QPushButton;  
  26.   startBtn->setText(tr("开始"));  
  27.   mainLayout = new QGridLayout(this);  
  28.   mainLayout->addWidget(fileNum, 0, 0, 1, 1);  
  29.   mainLayout->addWidget(fileNumLineEdit, 0, 1, 1, 1);  
  30.   mainLayout->addWidget(progressType, 1, 0, 1, 1);  
  31.   mainLayout->addWidget(comboBox, 1, 1, 1 ,1);  
  32.   mainLayout->addWidget(progressBar, 2, 1, 1, 2);  
  33.   mainLayout->addWidget(startBtn, 3, 1, 1, 1);  
  34.   mainLayout->setMargin(15);  
  35.   mainLayout->setSpacing(10);  
  36.   connect(startBtn, SIGNAL(clicked()), this, SLOT(startProgress()));  
  37. }  
  38. void MyDlg::startProgress()  
  39. {  
  40.   bool ok;  
  41.   int num = fileNumLineEdit->text().toInt(&ok);  
  42.   if(!ok)  
  43.   {  
  44.       return ;  
  45.   }  
  46.   if(comboBox->currentIndex() == 0)  
  47.   {  
  48.       progressBar->setRange(0, num);  
  49.       for(int i = 1; i <= num; i++)  
  50.       {  
  51.           progressBar->setValue(i);  
  52.       }  
  53.   }  
  54.   else  
  55.   {  
  56.       QProgressDialog *progressDlg=new QProgressDialog(this);  
  57.       QFont font("ZYSong18030",12);  
  58.       progressDlg->setFont(font);  
  59.       progressDlg->setWindowModality(Qt::WindowModal);  
  60.       progressDlg->setMinimumDuration(5);  
  61.       progressDlg->setWindowTitle(tr("please wait"));  
  62.       progressDlg->setLabelText(tr("正在复制......     "));  
  63.       progressDlg->setCancelButtonText(tr("取消"));  
  64.       progressDlg->setRange(0,num);  
  65.       for(int i = 1; i <= num; i++)  
  66.       {  
  67.           progressDlg->setValue(i);  
  68.           if(progressDlg->wasCanceled())  
  69.           {  
  70.               return ;  
  71.           }  
  72.       }  
  73.   }  
  74. }  


 

 

  1. //main.cpp  
  2. #include <QApplication>  
  3. #include <QTextCodec>  
  4. #include "MyDlg.h"  
  5. int main(int argc, char *argv[])  
  6. {  
  7.   QTextCodec::setCodecForTr(QTextCodec::codecForLocale());  
  8.   QApplication app(argc, argv);  
  9.   MyDlg md;  
  10.   md.show();  
  11.   return app.exec();  
  12. }  


 

程序运行截图:

                     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值