QProgressBar的使用例子
今天下午动手实践了一下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的使用例子的更多相关文章
- 【PyQt5-Qt Designer】QProgressBar() 进度条
QProgressBar() 进度条 QProgressBar简介 QProgressBar小部件提供了一个水平或垂直的进度条. 进度条用于向用户指示操作的进度,并向他们保证应用程序仍在运行. 进度条 ...
- pyqt4_应用例子(计算器,对话框,进度条,日历等等)
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- 浏览器透明设置例子,qt5.6才支持
用simpleBrowser例子的基础上,在BrowserWindow构造函数修改如下 BrowserWindow::BrowserWindow(QWidget *parent, Qt::Window ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...
- REGEX例子
作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...
- CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子
CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...
- 简单例子了解View的事件分发
什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...
- 简单的例子了解自定义ViewGroup(一)
在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...
随机推荐
- ASP.NET MVC 模型和数据对象映射实践
在使用 MVC 开发项目的过程中遇到了个问题,就是模型和数据实体之间的如何快捷的转换?是不是可以像 Entity Framework 的那样 EntityTypeConfiguration,或者只需要 ...
- 判断 ACdream 1202 Integer in C++
题目传送门 /* 分情况讨论,在long long范围里可以直接比较 sscanf 直接读到n中去 */ #include <cstdio> #include <iostream&g ...
- LightOJ1360 Skyscraper(DP)
题目大概是,一个数轴上n个线段,每个线段都有起始坐标.长度和权值,问从中取出没有公共交点的线段的最大权和. 取k次是个经典的最小费用最大流问题,不过这题建容量网络有20W个点,离散化最多也要6W个点, ...
- Unity3D脚本18:可视化辅助设置类 Gizmos
Gizmos 类 Gizmos用于场景中给出一个可视化的调试或辅助设置. 所有的Gizmos绘制都必须在脚本的OnDrawGizmos或OnDrawGizmosSelected函数中完成. OnD ...
- ural 1286. Starship Travel
1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equ ...
- 【转】Eclipse Plugin 在 Console上打印出message
org.eclipse.ui.console 转自:http://blog.csdn.net/rujielaisusan/article/details/4505188 首先这里主要用到的是org.e ...
- 【转】执行脚本出现bin/bash: bad interpreter: No such file or directory
[转自]http://blog.csdn.net/wind19/article/details/4822666 错误原因之一很有可能是你的脚本文件是DOS格式的, 即每一行的行尾以/r/n来标识, 其 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...
- C#的泛型委托Predicate/Func/Action(转)
Predicate 泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. 类型参数介绍: T: 要比较的对 ...