Chinaunix首页 | 论坛 | 博客
  • 博客访问: 565481
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: C/C++

2014-07-24 17:00:37

原文地址:http://blog.csdn.net/a649518776/article/details/6648347
两种进度条显示:

QProgressBar和QProgressDialog

下面例子展示两种进度条:

     

代码:

头文件dialog.h中:

  1.     #include <QtGui/QDialog>
  2.     #include <QLabel>
  3.     #include <QLineEdit>
  4.     #include <QProgressBar>
  5.     #include <QComboBox>
  6.     #include <QPushButton>
  7.     #include <QGridLayout>
  8.     class Dialog : public QDialog
  9.     {
  10.         Q_OBJECT
  11.       
  12.     public:
  13.         Dialog(QWidget *parent = 0);
  14.         ~Dialog();
  15.     private slots:
  16.         void startProgress();
  17.     private:
  18.         QLabel *FileNum;
  19.         QLineEdit *FileNumLineEdit;
  20.         QLabel *ProgressType;
  21.         QComboBox *comboBox;
  22.         QProgressBar *progressBar;
  23.         QPushButton *startBtn;
  24.         QGridLayout *mainLayout;
  25.     }


源文件:

  1.     #include "dialog.h"
  2.     #include <QProgressDialog>
  3.     #include <QFont>
  4.       
  5.     Dialog::Dialog(QWidget *parent)
  6.         : QDialog(parent)
  7.     {
  8.         setWindowTitle(tr("Progress"));
  9.       
  10.         FileNum=new QLabel;
  11.         FileNum->setText(tr("文件数目:"));
  12.         FileNumLineEdit=new QLineEdit;
  13.         FileNumLineEdit->setText(tr("10000"));
  14.       
  15.         ProgressType=new QLabel;
  16.         ProgressType->setText(tr("显示类型:"));
  17.         comboBox=new QComboBox;
  18.         comboBox->addItem(tr("progressBar"));
  19.         comboBox->addItem(tr("progressDialog"));
  20.       
  21.         progressBar=new QProgressBar;
  22.       
  23.         startBtn=new QPushButton;
  24.         startBtn->setText(tr("开始"));
  25.       
  26.         mainLayout=new QGridLayout(this);
  27.         mainLayout->addWidget(FileNum,0,0);
  28.         mainLayout->addWidget(FileNumLineEdit,0,1);
  29.         mainLayout->addWidget(ProgressType,1,0);
  30.         mainLayout->addWidget(comboBox,1,1);
  31.         mainLayout->addWidget(progressBar,2,0,1,2);
  32.         mainLayout->addWidget(startBtn,3,1);
  33.         mainLayout->setMargin(15);
  34.         mainLayout->setSpacing(10);
  35.       
  36.         connect(startBtn,SIGNAL(clicked()),this,SLOT(startProgress()))
  37.     }


  1.     void Dialog::startProgress()
  2.     {
  3.         bool ok;
  4.         int num=FileNumLineEdit->text().toInt(&ok);
  5.         if(!ok)
  6.             return;
  7.         if(comboBox->currentIndex()==0)
  8.         {
  9.             progressBar->setRange(0,num);
  10.             for(int i=1;i<=num;i++)
  11.             {
  12.                 progressBar->setValue(i);
  13.             }
  14.         }
  15.         else if(comboBox->currentIndex()==1)
  16.         {
  17.             QProgressDialog *progressDlg=new QProgressDialog(this);
  18.             QFont font("ZYSong18030",12);
  19.             progressDlg->setFont(font);
  20.             progressDlg->setWindowModality(Qt::WindowModal);
  21.             progressDlg->setMinimumDuration(5);
  22.             progressDlg->setWindowTitle(tr("please wait"));
  23.             progressDlg->setLabelText(tr("正在复制...... "));
  24.             progressDlg->setCancelButtonText(tr("取消"));
  25.             progressDlg->setRange(0,num);
  26.             for(int i=1;i<=num;i++)
  27.             {
  28.                 progressDlg->setValue(i);
  29.                 if(progressDlg->wasCanceled())
  30.                     return;
  31.             }
  32.         }
  33.     }
main函数添加显示中文的代码:

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

阅读(7455) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~