赞
踩
简述:
Qt提供了两种显示进度条的方式:一种是QProgressBar,提供了一种横向或者纵向显示进度的控件表示方式,用来描述任务的完成情况;另一种是QProgressDialog,提供了一种针对慢速过程的进度对话框表示方式,用于描述任务完成的进度情况。标准的进度条对话框包括一个进度显示条、一个取消按钮及一个标签。
m_pConnectProBar = new QProgressBar;m_pConnectProBar->setRange(0,100); //设置进度条最小值和最大值(取值范围)m_pConnectProBar->setMinimum(0); //设置进度条最小值m_pConnectProBar->setMaximum(100); //设置进度条最大值m_pConnectProBar->setValue(50); //设置当前的运行值m_pConnectProBar->reset(); //让进度条重新回到开始m_pConnectProBar->setOrientation(Qt::Horizontal); //水平方向m_pConnectProBar->setOrientation(Qt::Vertical); //垂直方向m_pConnectProBar->setAlignment(Qt::AlignVCenter); // 对齐方式m_pConnectProBar->setTextVisible(false); //隐藏进度条文本m_pConnectProBar->setFixedSize(258,5); //进度条固定大小m_pConnectProBar->setInvertedAppearance(true); //true:反方向 false:正方向m_pConnectProBar->setVisible(false); //false:隐藏进度条 true:显示进度条
枚举QProgressBar::Direction :指定垂直进度条文本的读取方向
这个属性对水平进度条没有影响。默认情况下,读取方向为: QProgressBar::TopToBottom
当水平进度时,可以从左到右,也可以从右到左;同样,垂直显示进度时,可以从上到下,也可以从下到上。
QProgressBar *m_pLeftToRightProBar = new QProgressBar(this); m_pLeftToRightProBar->setOrientation(Qt::Horizontal); // 水平方向 m_pLeftToRightProBar->setMinimum(0); // 最小值 m_pLeftToRightProBar->setMaximum(100); // 最大值 m_pLeftToRightProBar->setValue(50); // 当前进度 QProgressBar *m_pRightToLeftProBar = new QProgressBar(this); m_pRightToLeftProBar->setOrientation(Qt::Horizontal); // 水平方向 m_pRightToLeftProBar->setMinimum(0); // 最小值 m_pRightToLeftProBar->setMaximum(100); // 最大值 m_pRightToLeftProBar->setValue(50); // 当前进度 m_pRightToLeftProBar->setInvertedAppearance(true); // 反方向
setFormat() :用于生成当前文本字串
%p% : 百分比,这是默认的显示方式
%v : 当前进度
%m : 总步数
QProgressBar *m_pProgressBar = new QProgressBar(this); m_pProgressBar->setOrientation(Qt::Horizontal); // 水平方向 m_pProgressBar->setMinimum(0); // 最小值 m_pProgressBar->setMaximum(4800); // 最大值 m_pProgressBar->setValue(2000); // 当前进度 double dProgress = (m_pProgressBar->value() - m_pProgressBar->minimum()) * 100.0 / (m_pProgressBar->maximum() - m_pProgressBar->minimum()); // 百分比计算公式 // m_pProgressBar->setFormat(QString::fromLocal8Bit("当前进度为:%1%").arg(QString::number(dProgress, 'f', 1))) m_pProgressBar->setFormat(tr("Current progress : %1%").arg(QString::number(dProgress, 'f', 1))); m_pProgressBar->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 对齐方式
如果要显示百分比,可以直接使用“%p%” (比如:41%);
setAlignment() ,可以指定显示文本的对齐方式;
如果最小值和最大值都设置为0,进度条会显示了一个繁忙指示,而不会显示当前的值。
QProgressBar *m_pProgressBar = new QProgressBar(this);m_pProgressBar->setOrientation(Qt::Horizontal); // 水平方向m_pProgressBar->setMinimum(0); // 最小值m_pProgressBar->setMaximum(0); // 最大值
头文件:progressdlg.h
#ifndef PROGRESSDLG_H #define PROGRESSDLG_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QProgressBar> #include <QComboBox> #include <QPushButton> #include <QGridLayout> class ProgressDlg : public QDialog { Q_OBJECT public: ProgressDlg(QWidget *parent = 0); ~ProgressDlg(); private slots: void startProgress(); private: QLabel *FileNum; QLineEdit *FileNumLineEdit; QLabel *ProgressType; QComboBox *comboBox; QProgressBar *progressBar; QPushButton *starBtn; QGridLayout *mainLayout; }; #endif // PROGRESSDLG_H
progressdlg.cpp文件:
#include "progressdlg.h" #include <QProgressDialog> #include <QFont> ProgressDlg::ProgressDlg(QWidget *parent) : QDialog(parent) { QFont font("ZYSong18030",12); setFont(font); setWindowTitle(tr("Progress")); FileNum =new QLabel; FileNum->setText(tr("文件数目:")); FileNumLineEdit =new QLineEdit; FileNumLineEdit->setText(tr("100000")); ProgressType =new QLabel; ProgressType->setText(tr("显示类型:")); comboBox =new QComboBox; comboBox->addItem(tr("progressBar")); comboBox->addItem(tr("progressDialog")); progressBar =new QProgressBar; starBtn =new QPushButton(); starBtn->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(starBtn,3,1); mainLayout->setMargin(15); mainLayout->setSpacing(10); connect(starBtn,SIGNAL(clicked()),this,SLOT(startProgress())); } ProgressDlg::~ProgressDlg() { } void ProgressDlg::startProgress() { bool ok; int num =FileNumLineEdit->text().toInt(&ok); if(comboBox->currentIndex()==0) //ProgressBar { progressBar->setRange(0,num); for(int i=1;i<num+1;i++) { progressBar->setValue(i); } } else if(comboBox->currentIndex()==1) //ProgressDialog { //创建一个进度对话框 QProgressDialog *progressDialog=new QProgressDialog(this); QFont font("ZYSong18030",12); progressDialog->setFont(font); //设置进度对话框采用模态方式进行,即显示进度的同时,其他窗口将不响应输入信号 progressDialog->setWindowModality(Qt::WindowModal); //设置进度对话框出现需等待的时间,默认为4s progressDialog->setMinimumDuration(5); //设置进度对话框的窗体标题 progressDialog->setWindowTitle(tr("Please Wait")); //设置进度对话框的显示文字信息 progressDialog->setLabelText(tr("Copying...")); //设置进度对话框的“取消”按钮的显示文字 progressDialog->setCancelButtonText(tr("Cancel")); progressDialog->setRange(0,num); //设置进度对话框的步进范围 for(int i=1;i<num+1;i++) { progressDialog->setValue(i); if(progressDialog->wasCanceled()) return; } } }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。