今天下午动手实践了一下QProgressBar,遇到的问题比较多,浪费了不少时间,但收获同样颇多... 程序界面如下:


 1 // progressbar.h
 2 
 3 #ifndef PROGRESSBAR_H
 4 #define PROGRESSBAR_H
 5 #include <QDialog>
 6 class QLabel;
 7 class QLineEdit;
 8 class QComboBox;
 9 class QPushButton;
10 class QProgressBar;
11 class Progress : public QDialog
12 {
13     Q_OBJECT
14 public:
15     Progress(QWidget *parent=0,Qt::WindowFlags f=0);
16     ~Progress();
17 private:
18     QLabel *numLabel;
19     QLineEdit *numLineEdit;
20     QLabel *typeLabel;
21     QComboBox *typeComboBox;
22     QProgressBar *progressBar;
23     QPushButton *startPushButton;
24 private slots:
25     void slotStart();
26 };
27 #endif // PROGRESSBAR_H

1 #include "progressbar.h"


 2 #include <QLabel>
 3 #include <QLineEdit>
 4 #include <QComboBox>
 5 #include <QProgressBar>
 6 #include <QPushButton>
 7 #include <QGridLayout>
 8 #include <QProgressDialog>
 9 #include <QApplication>
10 #include <QTest>                                           //以便调用QTest::qSleep(); 实现延时功能
11 
12 Progress::Progress(QWidget *parent,Qt::WindowFlags f):QDialog(parent,f)
13 {
14     QFont font("Times", 10, QFont::Bold);
15     setFont(font);
16     setWindowTitle("ProgressBar");
17     numLabel=new QLabel("File Num:");
18     numLineEdit=new QLineEdit;
19     numLineEdit->setText("10");
20     typeLabel=new QLabel("Progress Type:");
21     typeComboBox=new QComboBox;
22     typeComboBox->addItem("ProgressBar");
23     typeComboBox->addItem("ProgressDialog");
24 
25     progressBar=new QProgressBar;
26     startPushButton=new QPushButton("Start");
27 
28     QGridLayout *layout=new QGridLayout;
29     layout->addWidget(numLabel,0,0);
30     layout->addWidget(numLineEdit,0,1);
31     layout->addWidget(typeLabel,1,0);
32     layout->addWidget(typeComboBox,1,1);
33     layout->addWidget(progressBar,2,0,1,2);
34     layout->addWidget(startPushButton,3,1);
35     layout->setMargin(15);                                                           // 布局外边框的宽度
36     layout->setSpacing(10);                                                         //部件间的间距
37 
38     setLayout(layout);
39 
40     connect(startPushButton,SIGNAL(clicked()),this,SLOT(slotStart()));
41 }
42 
43 Progress::~Progress(){}
44 
45 void Progress::slotStart()
46 {
47     int num=numLineEdit->text().toInt();
48     if (typeComboBox->currentIndex()==0)
49     {
50         progressBar->setRange(0,num);
51         for (int i=0;i<num;i++)
52         {
53             progressBar->setValue(i+1);                     // 注意,这里是i+1,不是i,这样才能显示100%
54             QTest::qSleep(100);
55         }
56     }
57     else if (typeComboBox->currentIndex()==1)
58     {
59         QProgressDialog *progressDialog=new QProgressDialog(this);
60         QFont font("Times", 10, QFont::Bold);
61         progressDialog->setFont(font);
62         progressDialog->setWindowModality(Qt::WindowModal);       //设置窗口模式,这里复制对话框弹出后,无法操作父窗口
63         progressDialog->setMinimumDuration(5);                            //设置任务执行的预期时间,若小于此值,就不弹出复制对话框
64         progressDialog->setWindowTitle("Please Wait...");
65         progressDialog->setLabelText("Copying...");
66         progressDialog->setCancelButtonText("Cancel");
67         progressDialog->setRange(0,num);
68 
69         for (int i=0;i<num;i++)
70         {
71             progressDialog->setValue(i+1);                 // 注意,这里是i+1,不是i,这样才能显示100%
72             qApp->processEvents();                         //来正常相应时间循环,以确保应用程序不会出现阻塞。
73             QTest::qSleep(100);
74             if (progressDialog->wasCanceled()) return;
75         }
76     }
77 }
这个程序只是个实例,模拟文件的复制过程,这里用到了延时函数qSleep(),它包含于QTest类中,在使用它之前,首先要在pro文件中加入:
CONFIG +=qtestlib

注意,在加入上述声明后,运行程序以后会弹出cmd窗口,这个是正常的,QTest用来测试,默认就带cmd黑色窗口,我一开不了解,四处找去掉的原因,在实际运用过程中,我们不需要QTest类,只需要将实际的文件复制代码覆盖程序中的QTest::qSleep(100);即可...

(其实实现延时功能,用QTimer方法更好,当时就是没往这块考虑,呵呵)

http://www.cnblogs.com/hicjiajia/archive/2010/08/27/1809984.html

QProgressBar的使用例子的更多相关文章

  1. 【PyQt5-Qt Designer】QProgressBar() 进度条

    QProgressBar() 进度条 QProgressBar简介 QProgressBar小部件提供了一个水平或垂直的进度条. 进度条用于向用户指示操作的进度,并向他们保证应用程序仍在运行. 进度条 ...

  2. pyqt4_应用例子(计算器,对话框,进度条,日历等等)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  3. 浏览器透明设置例子,qt5.6才支持

    用simpleBrowser例子的基础上,在BrowserWindow构造函数修改如下 BrowserWindow::BrowserWindow(QWidget *parent, Qt::Window ...

  4. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  5. C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  6. REGEX例子

    作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...

  7. CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子

    CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...

  8. 简单例子了解View的事件分发

    什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...

  9. 简单的例子了解自定义ViewGroup(一)

    在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...

随机推荐

  1. node连接--MySQL

    MySQL驱动器:node-mysql; MySQL对象关系映射器:node-sequelize; 例子: package.json: { "name": "shoppi ...

  2. Repeatless Numbers[POJ2956]

    Repeatless Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1719   Accepted: 726 ...

  3. 策略模式c++【转】

    作用:定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. UML图: Strategy模式将逻辑(算法)封装到一个类(Context)里面,通过组合的 ...

  4. javascript,HTML,PHP,ASP做301跳转代码 SEO优化设置

    URL HTTP Redirection URL http redirection is an automatic URL change operation from one URL to anoth ...

  5. Channel 笔记本项目 (门户客户端 和 wp7客户端(介绍1))

    Channel 笔记本项目:(所包含 门户客户端 和 wp7客户端)              首先wp7客户端中,首页向右滑行,到了新闻(博文):(点触某篇新闻后,进入到新闻详细页面,在菜单栏所对应 ...

  6. 【BZOJ】2209: [Jsoi2011]括号序列(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2209 splay又犯逗........upd1那里的sum忘记赋值反............. 本题 ...

  7. 【POJ】A New Stone Game(博弈论)

    http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...

  8. android开发 BaseAdapter中getView()里的3个参数是什么意思

    BaseAdapter适配器里有个getView()需要重写public View getView(int position,View converView,ViewGroup parent){ // ...

  9. python中的循环

    >>> x = 100 >>> y = 10>>> x < y and x or y10>>> x if x > y ...

  10. 新鲜出炉的30个精美的 jQuery & CSS3 效果【附演示和教程】

    新鲜出炉的30个精美的 jQuery & CSS3 效果[附演示和教程]   作为最流行的 JavaScript 开发框架,jQuery 在现在的 Web 开发项目中扮演着重要角色,它简化了 ...