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 <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方法更好,当时就是没往这块考虑,呵呵)



posted on 2012-03-21 21:26  风行雪舞  阅读(1364)  评论(0编辑  收藏  举报
无觅相关文章插件,快速提升流量