【Qt5开发及实例】10、关于进度条的显示

平常我们下载东西总会有一个显示下载了多少的进度条,我们今天来实现一下。

这里有两种显示方式

可以选择,第一个是用了

QProgressBar控件,第二个是用了QProgressDialog控件

progressdlg.h

/**
* 书本:【Qt5开发及实例】
* 功能:为了实现进度条的显示
* 文件:progressdlg.h
* 时间:2015年1月2日15:27:10
* 作者:cutter_point
*/
#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 *startBtn;
  QGridLayout *mainLayout;

};

#endif // PROGRESSDLG_H

progressdlg.cpp

/**
* 书本:【Qt5开发及实例】
* 功能:完成界面的显示的定义
* 文件:progressdlg.cpp
* 时间:2015年1月2日15:27:37
* 作者:cutter_point
*/
#include "progressdlg.h"

#include <QProgressDialog>    //这个显示进度条的慢速过程的进度框
#include <QFont>
#include <iostream>

using namespace std;

ProgressDlg::ProgressDlg(QWidget *parent)
  : QDialog(parent)
{

  //完成界面的初始化
  QFont font("宋体", 12);
  setFont(font);
  setWindowTitle(tr("Progress"));

  FileNum = new QLabel;
  FileNum->setText(tr("the file num"));   //文件的数目
  FileNumLineEdit = new QLineEdit;
  FileNumLineEdit->setText(tr("100000"));   //默认值

  ProgressType = new QLabel;
  ProgressType->setText(tr("the show type"));     //显示的类型
  comboBox = new QComboBox;
  comboBox->addItem(tr("progressBar"));   //第一种显示方式
  comboBox->addItem(tr("progressDialog"));      //第二种显示方式

  progressBar = new QProgressBar;
  progressBar->setFormat("%p%");    //按完成的百分比显示

  startBtn = new QPushButton;
  startBtn->setText(tr("begin"));    //开始

//  cout<<"???3333"<<endl;

  mainLayout = new QGridLayout(this);   //网格布局
//  cout<<"???3333____?????11111"<<endl;
  mainLayout->addWidget(FileNum, 0, 0);
  mainLayout->addWidget(FileNumLineEdit, 0, 1);
//  cout<<"???33333__?????1111111___???222222"<<endl;
  mainLayout->addWidget(ProgressType, 1, 0);

//  cout<<"???3333____??????22222"<<endl;

  mainLayout->addWidget(comboBox, 1, 1);
  mainLayout->addWidget(progressBar, 2, 0, 1, 2);
  mainLayout->addWidget(startBtn, 3, 1);

//  cout<<"???3333____111111111"<<endl;

  mainLayout->setMargin(15);    //设置间隔大小
  mainLayout->setSpacing(10);

//   cout<<"???4444"<<endl;

  //连接
  connect(startBtn, SIGNAL(clicked()), this, SLOT(startProgress()));

}

//进度条的具体工作显示槽函数
void ProgressDlg::startProgress()
{
  bool ok;
  int num = FileNumLineEdit->text().toInt(&ok);     //把文本转换为int类型值

  if(comboBox->currentIndex() == 0)     //如果选择的是第一个的话ProgressBar模式
    {
      progressBar->setRange(0, num);      //设置范围,最小值和最大值
      for(int i = 1; i <= num; ++i)
      {
        progressBar->setValue(i);     //设置当前值
        cout<<i<<endl;
      }
    }
  else if(comboBox->currentIndex() == 1)    //如果选择的是第二个显示方式的话, ProgressDialog
    {
         QProgressDialog *progressDialog = new QProgressDialog(this);
         QFont font("宋体", 12);
         progressDialog->setFont(font);
         progressDialog->setWindowModality(Qt::WindowModal);      //采用模拟的方式进行显示,即显示进度的同时,其他窗口将不响应输入信号
         progressDialog->setMinimumDuration(5000);   //设置进度条显示的等待时间,5秒
         progressDialog->setWindowTitle(tr("Please Wait"));   //设置标题的显示时间
         progressDialog->setLabelText(tr("Copying..."));
         progressDialog->setCancelButtonText(tr("Cancel"));     //退出按钮名字
         progressDialog->setRange(0, num);    //设置显示的范围

         for(int i =  1; i <= num; ++i)
           {
             progressDialog->setValue(i);   //设置当前的值
//             cout<<i<<endl;
             //如果检测到按钮取消被激活,就跳出去
             if(progressDialog->wasCanceled())
               return;

           }

    }

}

ProgressDlg::~ProgressDlg()
{

}

main.cpp

#include "progressdlg.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  ProgressDlg w;
  w.show();

  return a.exec();
}

OK ,单纯为了大家爽一爽!!

让你开心,让你笑

我去吧项目上传一下,下次给连接吧

连接:欲知后事如何,且听下回分解。

时间: 2024-12-19 18:37:31

【Qt5开发及实例】10、关于进度条的显示的相关文章

【Qt5开发及实例】24、数据柱形图显示

数据柱形图显示 1.我们首先把这个这个视图的表格部分表示出来 mainwindow.h /** * 书本:[Qt5开发及实例] * 功能:数据柱形图显示,这个类是表格显示 * 文件:mainwindow.h * 时间:2015年1月28日18:50:54 * 作者:cutter_point */ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QStandardItemModel>

【Qt5开发及实例】21、导弹地图演示

导弹地图演示 这个导弹地图是一个中国的地图: 好的废话不多直接上代码(里面有大量注释,不怕你看不懂) main.cpp /** * 书本:[Qt5开发及实例] * 功能:实现地图的浏览器 * 文件:main.cpp * 时间:2015年1月26日18:57:55 * 作者:cutter_point */ #include "mapwidget.h" #include <QApplication> #include <QFont> int main(int arg

【Qt5开发及实例】12、实现一个简单的文本编辑器1

showwidget.h /** * 书本:[Qt5开发及实例] * 功能:实现一个简单的文本编辑器 * 文件:showwidget.h * 时间:2015年1月18日10:03:21 * 作者:cutter_point */ #ifndef SHOWWIDGET_H #define SHOWWIDGET_H #include <QWidget> #include <QLabel> #include <QTextEdit> #include <QImage>

【Qt5开发及实例】16、实现一个简单的文本编辑器(over)

实现一个简单的文本编辑器 其他具体的代码基础看前面:http://blog.csdn.net/cutter_point/article/details/42839071 1.功能 这个程序又添加了文本编辑功能,什么加粗,斜体,下划线,字体设置,字号设置,文字排版,段落对齐功能. 2.代码全展示 头文件 showwidget.h /** * 书本:[Qt5开发及实例] * 功能:实现一个简单的文本编辑器 * 文件:showwidget.h * 时间:2015年1月18日10:03:21 * 作者:

【Qt5开发及实例】18、图形修饰小工具

图形修饰小工具 无力吐槽,这是我第三遍写这个了,到底是个什么意思???我只要一贴代码,浏览器直接崩溃,呵呵了,我也是,我现在只要写完一段字我就保存,尼玛在掉我就不写了,写到word里面,再贴上来. 效果 左边图形展示界面 paintarea.h /** * 书本:[Qt5开发及实例] * 功能:实现绘画各种图形 * 文件:paintarea.h * 时间:2015年1月21日16:59:25 * 作者:cutter_point */ #ifndef PAINTAREA_H #define PAI

【Qt5开发及实例】30、实现客户端的编程,UDP协议

udpclient.h /** * 书本:[Qt5开发及实例] * 功能:实现客户端的编程 * 文件:udpclient.h * 时间:2015年2月5日22:10:30 * 作者:cutter_point */ #ifndef UDPCLIENT_H #define UDPCLIENT_H #include <QDialog> #include <QVBoxLayout> #include <QTextEdit> #include <QPushButton>

【Qt5开发及实例】8、各种对话框!!

1.标准文件对话框 就是点击这个按钮就会打开文件的对话框 具体的实现是: 头文件dialog.h: #include <QDialog> #include <QLineEdit> #include <QGridLayout> //网格布局 #include <QPushButton> #include <iostream> #include "inputdlg.h" #include "msgboxdlg.h&quo

【Qt5开发及实例】9、一个QQ界面

这个程序主要是为了学习一下工具盒类,这里使用了3个工具盒然后添加到整个界面上, 分别是:好友(good friend), 陌生人(do not remember), 黑名单(black items) 哈哈,原谅我这不咋地的英语,懂我就好. 实现图: 代码: drawer.h /** * 书本:[Qt5开发及实例] * 功能:实现一个自己的QQ界面 * 文件:drawer.cpp * 时间:2015年1月2日11:01:12 * 作者:cutter_point */ #ifndef DRAWER_

【Qt5开发及实例】25、实现代理的功能

实现代理的功能 在Qt里面也有MVC,那就是视图,模型,代理,后面我们再开一章,好好来学习一下Qt的MVC吧! main.cpp /** * 书本:[Qt5开发及实例] * 功能:实现代理的功能 * 文件:main.cpp * 时间:2015年1月29日20:53:04 * 作者:cutter_point */ #include <QApplication> #include <QStandardItemModel> #include <QTableView> //#i