QT界面开发-QProgressBar【转载】

转载自https://blog.csdn.net/hebbely/article/details/61418591

简述:

Qt提供了两种显示进度条的方式:一种是QProgressBar,提供了一种横向或者纵向显示进度的控件表示方式,用来描述任务的完成情况;另一种是QProgressDialog,提供了一种针对慢速过程的进度对话框表示方式,用于描述任务完成的进度情况。标准的进度条对话框包括一个进度显示条、一个取消按钮及一个标签。

1、 QProgressBar基本用法

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:显示进度条

2、 读取方向

枚举QProgressBar::Direction  :指定垂直进度条文本的读取方向

 这个属性对水平进度条没有影响。默认情况下,读取方向为: QProgressBar::TopToBottom

3、进度方向

当水平进度时,可以从左到右,也可以从右到左;同样,垂直显示进度时,可以从上到下,也可以从下到上。

    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);  // 反方向

4、文本显示

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() ,可以指定显示文本的对齐方式;

5、繁忙指示

如果最小值和最大值都设置为0,进度条会显示了一个繁忙指示,而不会显示当前的值。

QProgressBar *m_pProgressBar = new QProgressBar(this);
m_pProgressBar->setOrientation(Qt::Horizontal); // 水平方向
m_pProgressBar->setMinimum(0); // 最小值
m_pProgressBar->setMaximum(0); // 最大值

6、两种显示进度条的方式:QProgressBar和QProgressDialog

 头文件:progressdlg.h

 1 #ifndef PROGRESSDLG_H
 2 #define PROGRESSDLG_H
 3  
 4 #include <QDialog>
 5 #include <QLabel>
 6 #include <QLineEdit>
 7 #include <QProgressBar>
 8 #include <QComboBox>
 9 #include <QPushButton>
10 #include <QGridLayout>
11  
12 class ProgressDlg : public QDialog
13 {
14     Q_OBJECT
15     
16 public:
17     ProgressDlg(QWidget *parent = 0);
18     ~ProgressDlg();
19 private slots:
20     void startProgress();
21 private:
22     QLabel *FileNum;
23     QLineEdit *FileNumLineEdit;
24     QLabel *ProgressType;
25     QComboBox *comboBox;
26     QProgressBar *progressBar;
27     QPushButton *starBtn;
28     QGridLayout *mainLayout;
29 };
30  
31 #endif // PROGRESSDLG_H

progressdlg.cpp文件:

 1 #include "progressdlg.h"
 2 #include <QProgressDialog>
 3 #include <QFont>
 4 ProgressDlg::ProgressDlg(QWidget *parent)
 5     : QDialog(parent)
 6 {
 7     QFont font("ZYSong18030",12);
 8     setFont(font);
 9     setWindowTitle(tr("Progress"));
10  
11     FileNum =new QLabel;
12     FileNum->setText(tr("文件数目:"));
13     FileNumLineEdit =new QLineEdit;
14     FileNumLineEdit->setText(tr("100000"));
15  
16     ProgressType =new QLabel;
17     ProgressType->setText(tr("显示类型:"));
18     comboBox =new QComboBox;
19     comboBox->addItem(tr("progressBar"));
20     comboBox->addItem(tr("progressDialog"));
21  
22     progressBar =new QProgressBar;
23  
24     starBtn =new QPushButton();
25     starBtn->setText(tr("开始"));
26  
27     mainLayout =new QGridLayout(this);
28     mainLayout->addWidget(FileNum,0,0);
29     mainLayout->addWidget(FileNumLineEdit,0,1);
30     mainLayout->addWidget(ProgressType,1,0);
31     mainLayout->addWidget(comboBox,1,1);
32     mainLayout->addWidget(progressBar,2,0,1,2);
33     mainLayout->addWidget(starBtn,3,1);
34     mainLayout->setMargin(15);
35     mainLayout->setSpacing(10);
36  
37     connect(starBtn,SIGNAL(clicked()),this,SLOT(startProgress()));
38 }
39  
40 ProgressDlg::~ProgressDlg()
41 {
42     
43 }
44  
45 void ProgressDlg::startProgress()
46 {
47     bool ok;
48     int num =FileNumLineEdit->text().toInt(&ok);
49  
50     if(comboBox->currentIndex()==0)                   //ProgressBar
51     {
52         progressBar->setRange(0,num);
53         for(int i=1;i<num+1;i++)
54         {
55             progressBar->setValue(i);
56         }
57     }
58     else if(comboBox->currentIndex()==1)             //ProgressDialog
59     {
60         //创建一个进度对话框
61         QProgressDialog *progressDialog=new QProgressDialog(this);
62         QFont font("ZYSong18030",12);
63         progressDialog->setFont(font);
64         //设置进度对话框采用模态方式进行,即显示进度的同时,其他窗口将不响应输入信号
65         progressDialog->setWindowModality(Qt::WindowModal);
66         //设置进度对话框出现需等待的时间,默认为4s
67         progressDialog->setMinimumDuration(5);
68         //设置进度对话框的窗体标题
69         progressDialog->setWindowTitle(tr("Please Wait"));
70         //设置进度对话框的显示文字信息
71         progressDialog->setLabelText(tr("Copying..."));
72         //设置进度对话框的“取消”按钮的显示文字
73         progressDialog->setCancelButtonText(tr("Cancel"));
74         progressDialog->setRange(0,num); //设置进度对话框的步进范围
75         for(int i=1;i<num+1;i++)
76         {
77             progressDialog->setValue(i);
78             if(progressDialog->wasCanceled())
79                 return;
80         }
81     }
82 }

猜你喜欢

转载自www.cnblogs.com/nxopen2018/p/12218184.html
今日推荐