博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt的进度条实现
阅读量:7027 次
发布时间:2019-06-28

本文共 3285 字,大约阅读时间需要 10 分钟。

1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 // progressbar.h  2   3  #ifndef PROGRESSBAR_H  4  #define PROGRESSBAR_H  5  #include 
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 
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
//以便调用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
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
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://tmoxl.baihongyu.com/

你可能感兴趣的文章
MSSQL数据库迁移到Oracle(二)
查看>>
S3C2440触摸屏控制总结
查看>>
视频文件格式
查看>>
文件异步上传方式(一)
查看>>
funny alphabet
查看>>
STL队列 之FIFO队列(queue)、优先队列(priority_queue)、双端队列(deque)
查看>>
Android压力测试工具——Monkey
查看>>
使用“DiskGenius”精确隐藏硬盘坏道
查看>>
我心中的核心组件(可插拔的AOP)~第十二回 IoC组件Unity
查看>>
Spring3系列4-多个配置文件的整合
查看>>
SQLServer2005重建索引前后对比【转】
查看>>
Inode详解
查看>>
jquery加入收藏代码
查看>>
7z命令行工具
查看>>
AutoCompleteTextView 与sqlite绑定实现记住用户输入的内容并自动提示
查看>>
Makefile 中会在多处地方看到 FORCE
查看>>
hadoop参数传递
查看>>
揭秘uc浏览器四
查看>>
用条件注释判断浏览器版本,解决兼容问题
查看>>
通过IEnumerable和IDisposable实现可暂停和取消的任务队列
查看>>