Qt progress bar implementation

Source: Internet
Author: User
Tags qtest
 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> // call qtest: qsleep (); To implement the latency Function
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); // width of the outer border of the layout
36 Layout-> setspacing (10); // spacing between Parts
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); // Note: Here is I + 1, not I, so that 100% is displayed
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); // set the window mode. After the copy dialog box is displayed, the parent window cannot be operated.
63 progressdialog-> setminimumduration (5); // set the expected time for task execution. If the value is smaller than this value, the copy dialog box is not displayed.
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); // Note: Here is I + 1, not I, so that 100% is displayed
72 qapp-> processevents (); // you can use the corresponding time loop to ensure that the application is not blocked.
73 qtest: qsleep (100 );
74 if (progressdialog-> wascanceled () return;
75}
76}
77}
This program is just an instance to simulate the file copy process. Here we use the latency function qsleep (), which is included in the qtest class. before using it, first, add the following content to the Pro file:
CONFIG +=qtestlib

Note: After the above declaration is added, the CMD window will pop up after the program is run. This is normal. qtest is used for testing. By default, the CMD black window is displayed, find out the reason for removing the file. In actual use, we do not need the qtest class. We only need to copy the actual file code to overwrite the qtest: qsleep (100) in the program ); you can...

 

 

(In fact, it is better to use the qtimer method to implement the latency function. At that time, I did not consider it here)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.