1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/progressbar/progressbar.doc:5 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Progress Bar</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Progress Bar</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example shows how to use a progress bar.
|
---|
37 | <p> <hr>
|
---|
38 | <p> Header file:
|
---|
39 | <p> <pre>/****************************************************************************
|
---|
40 | ** $Id: progressbar-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
41 | **
|
---|
42 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
43 | **
|
---|
44 | ** This file is part of an example program for Qt. This example
|
---|
45 | ** program may be used, distributed and modified without limitation.
|
---|
46 | **
|
---|
47 | *****************************************************************************/
|
---|
48 |
|
---|
49 | #ifndef PROGRESSBAR_H
|
---|
50 | #define PROGRESSBAR_H
|
---|
51 |
|
---|
52 | #include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>
|
---|
53 | #include <<a href="qtimer-h.html">qtimer.h</a>>
|
---|
54 |
|
---|
55 | class QRadioButton;
|
---|
56 | class QPushButton;
|
---|
57 | class QProgressBar;
|
---|
58 |
|
---|
59 | class ProgressBar : public <a href="qbuttongroup.html">QButtonGroup</a>
|
---|
60 | {
|
---|
61 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
62 |
|
---|
63 | public:
|
---|
64 | ProgressBar( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
|
---|
65 |
|
---|
66 | protected:
|
---|
67 | <a href="qradiobutton.html">QRadioButton</a> *slow, *normal, *fast;
|
---|
68 | <a href="qpushbutton.html">QPushButton</a> *start, *pause, *reset;
|
---|
69 | <a href="qprogressbar.html">QProgressBar</a> *progress;
|
---|
70 | <a href="qtimer.html">QTimer</a> timer;
|
---|
71 |
|
---|
72 | protected slots:
|
---|
73 | void slotStart();
|
---|
74 | void slotReset();
|
---|
75 | void slotTimeout();
|
---|
76 |
|
---|
77 | };
|
---|
78 |
|
---|
79 | #endif
|
---|
80 | </pre>
|
---|
81 |
|
---|
82 | <p> <hr>
|
---|
83 | <p> Implementation:
|
---|
84 | <p> <pre>/****************************************************************************
|
---|
85 | ** $Id: progressbar-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
86 | **
|
---|
87 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
88 | **
|
---|
89 | ** This file is part of an example program for Qt. This example
|
---|
90 | ** program may be used, distributed and modified without limitation.
|
---|
91 | **
|
---|
92 | *****************************************************************************/
|
---|
93 |
|
---|
94 | #include "progressbar.h"
|
---|
95 |
|
---|
96 | #include <<a href="qradiobutton-h.html">qradiobutton.h</a>>
|
---|
97 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
98 | #include <<a href="qprogressbar-h.html">qprogressbar.h</a>>
|
---|
99 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
100 |
|
---|
101 | #include <<a href="qmotifstyle-h.html">qmotifstyle.h</a>>
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Constructor
|
---|
105 | *
|
---|
106 | * Creates child widgets of the ProgressBar widget
|
---|
107 | */
|
---|
108 |
|
---|
109 | <a name="f346"></a>ProgressBar::ProgressBar( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
110 | : <a href="qbuttongroup.html">QButtonGroup</a>( 0, Horizontal, "Progress Bar", parent, name ), timer()
|
---|
111 | {
|
---|
112 | <a href="qframe.html#setMargin">setMargin</a>( 10 );
|
---|
113 |
|
---|
114 | <a href="qgridlayout.html">QGridLayout</a>* toplayout = new <a href="qgridlayout.html">QGridLayout</a>( <a href="qwidget.html#layout">layout</a>(), 2, 2, 5);
|
---|
115 |
|
---|
116 | <a href="qbuttongroup.html#setRadioButtonExclusive">setRadioButtonExclusive</a>( TRUE );
|
---|
117 |
|
---|
118 | // insert three radiobuttons which the user can use
|
---|
119 | // to set the speed of the progress and two pushbuttons
|
---|
120 | // to start/pause/continue and reset the progress
|
---|
121 | slow = new <a href="qradiobutton.html">QRadioButton</a>( "S&low", this );
|
---|
122 | normal = new <a href="qradiobutton.html">QRadioButton</a>( "&Normal", this );
|
---|
123 | fast = new <a href="qradiobutton.html">QRadioButton</a>( "&Fast", this );
|
---|
124 | <a href="qvboxlayout.html">QVBoxLayout</a>* vb1 = new <a href="qvboxlayout.html">QVBoxLayout</a>;
|
---|
125 | <a name="x964"></a> toplayout-><a href="qgridlayout.html#addLayout">addLayout</a>( vb1, 0, 0 );
|
---|
126 | vb1-><a href="qboxlayout.html#addWidget">addWidget</a>( slow );
|
---|
127 | vb1-><a href="qboxlayout.html#addWidget">addWidget</a>( normal );
|
---|
128 | vb1-><a href="qboxlayout.html#addWidget">addWidget</a>( fast );
|
---|
129 |
|
---|
130 | // two push buttons, one for start, for for reset.
|
---|
131 | start = new <a href="qpushbutton.html">QPushButton</a>( "&Start", this );
|
---|
132 | reset = new <a href="qpushbutton.html">QPushButton</a>( "&Reset", this );
|
---|
133 | <a href="qvboxlayout.html">QVBoxLayout</a>* vb2 = new <a href="qvboxlayout.html">QVBoxLayout</a>;
|
---|
134 | toplayout-><a href="qgridlayout.html#addLayout">addLayout</a>( vb2, 0, 1 );
|
---|
135 | vb2-><a href="qboxlayout.html#addWidget">addWidget</a>( start );
|
---|
136 | vb2-><a href="qboxlayout.html#addWidget">addWidget</a>( reset );
|
---|
137 |
|
---|
138 | // Create the progressbar
|
---|
139 | progress = new <a href="qprogressbar.html">QProgressBar</a>( 100, this );
|
---|
140 | <a name="x975"></a> // progress-><a href="qwidget.html#setStyle">setStyle</a>( new <a href="qmotifstyle.html">QMotifStyle</a>() );
|
---|
141 | <a name="x965"></a> toplayout-><a href="qgridlayout.html#addMultiCellWidget">addMultiCellWidget</a>( progress, 1, 1, 0, 1 );
|
---|
142 |
|
---|
143 | // connect the clicked() SIGNALs of the pushbuttons to SLOTs
|
---|
144 | <a name="x962"></a> <a href="qobject.html#connect">connect</a>( start, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotStart() ) );
|
---|
145 | <a href="qobject.html#connect">connect</a>( reset, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotReset() ) );
|
---|
146 |
|
---|
147 | // connect the timeout() SIGNAL of the progress-timer to a SLOT
|
---|
148 | <a href="qobject.html#connect">connect</a>( &timer, SIGNAL( timeout() ), this, SLOT( slotTimeout() ) );
|
---|
149 |
|
---|
150 | // Let's start with normal speed...
|
---|
151 | <a name="x972"></a> normal-><a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );
|
---|
152 |
|
---|
153 |
|
---|
154 | // some contraints
|
---|
155 | <a name="x974"></a> start-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( 80 );
|
---|
156 | <a href="qwidget.html#setMinimumWidth">setMinimumWidth</a>( 300 );
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * SLOT slotStart
|
---|
161 | *
|
---|
162 | * This SLOT is called if the user clicks start/pause/continue
|
---|
163 | * button
|
---|
164 | */
|
---|
165 |
|
---|
166 | void <a name="f347"></a>ProgressBar::slotStart()
|
---|
167 | {
|
---|
168 | // If the progress bar is at the beginning...
|
---|
169 | <a name="x966"></a> if ( progress-><a href="qprogressbar.html#progress">progress</a>() == -1 ) {
|
---|
170 | // ...set according to the checked speed-radiobutton
|
---|
171 | // the number of steps which are needed to complete the process
|
---|
172 | <a name="x971"></a> if ( slow-><a href="qradiobutton.html#isChecked">isChecked</a>() )
|
---|
173 | <a name="x969"></a> progress-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 10000 );
|
---|
174 | else if ( normal-><a href="qradiobutton.html#isChecked">isChecked</a>() )
|
---|
175 | progress-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 1000 );
|
---|
176 | else
|
---|
177 | progress-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 50 );
|
---|
178 |
|
---|
179 | // disable the speed-radiobuttons
|
---|
180 | <a name="x973"></a> slow-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
181 | normal-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
182 | fast-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
183 | }
|
---|
184 |
|
---|
185 | // If the progress is not running...
|
---|
186 | if ( !timer.isActive() ) {
|
---|
187 | // ...start the timer (and so the progress) with a interval of 1 ms...
|
---|
188 | timer.start( 1 );
|
---|
189 | // ...and rename the start/pause/continue button to Pause
|
---|
190 | <a name="x963"></a> start-><a href="qbutton.html#setText">setText</a>( "&Pause" );
|
---|
191 | } else { // if the prgress is running...
|
---|
192 | // ...stop the timer (and so the prgress)...
|
---|
193 | timer.stop();
|
---|
194 | // ...and rename the start/pause/continue button to Continue
|
---|
195 | start-><a href="qbutton.html#setText">setText</a>( "&Continue" );
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * SLOT slotReset
|
---|
201 | *
|
---|
202 | * This SLOT is called when the user clicks the reset button
|
---|
203 | */
|
---|
204 |
|
---|
205 | void <a name="f348"></a>ProgressBar::slotReset()
|
---|
206 | {
|
---|
207 | // stop the timer and progress
|
---|
208 | timer.stop();
|
---|
209 |
|
---|
210 | // rename the start/pause/continue button to Start...
|
---|
211 | start-><a href="qbutton.html#setText">setText</a>( "&Start" );
|
---|
212 | // ...and enable this button
|
---|
213 | start-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
214 |
|
---|
215 | // enable the speed-radiobuttons
|
---|
216 | slow-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
217 | normal-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
218 | fast-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
219 |
|
---|
220 | // reset the progressbar
|
---|
221 | <a name="x967"></a> progress-><a href="qprogressbar.html#reset">reset</a>();
|
---|
222 | }
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * SLOT slotTimeout
|
---|
226 | *
|
---|
227 | * This SLOT is called each ms when the timer is
|
---|
228 | * active (== progress is running)
|
---|
229 | */
|
---|
230 |
|
---|
231 | void <a name="f349"></a>ProgressBar::slotTimeout()
|
---|
232 | {
|
---|
233 | int p = progress-><a href="qprogressbar.html#progress">progress</a>();
|
---|
234 |
|
---|
235 | #if 1
|
---|
236 | // If the progress is complete...
|
---|
237 | <a name="x970"></a> if ( p == progress-><a href="qprogressbar.html#totalSteps">totalSteps</a>() ) {
|
---|
238 | // ...rename the start/pause/continue button to Start...
|
---|
239 | start-><a href="qbutton.html#setText">setText</a>( "&Start" );
|
---|
240 | // ...and disable it...
|
---|
241 | start-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
242 | // ...and return
|
---|
243 | return;
|
---|
244 | }
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | // If the process is not complete increase it
|
---|
248 | <a name="x968"></a> progress-><a href="qprogressbar.html#setProgress">setProgress</a>( ++p );
|
---|
249 | }
|
---|
250 | </pre>
|
---|
251 |
|
---|
252 | <p> <hr>
|
---|
253 | <p> Main:
|
---|
254 | <p> <pre>/****************************************************************************
|
---|
255 | ** $Id: progressbar-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
256 | **
|
---|
257 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
258 | **
|
---|
259 | ** This file is part of an example program for Qt. This example
|
---|
260 | ** program may be used, distributed and modified without limitation.
|
---|
261 | **
|
---|
262 | *****************************************************************************/
|
---|
263 |
|
---|
264 | #include "progressbar.h"
|
---|
265 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
266 |
|
---|
267 | int main(int argc,char **argv)
|
---|
268 | {
|
---|
269 | <a href="qapplication.html">QApplication</a> a(argc,argv);
|
---|
270 |
|
---|
271 | ProgressBar progressbar;
|
---|
272 | progressbar.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - ProgressBar");
|
---|
273 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(&progressbar);
|
---|
274 | progressbar.<a href="qwidget.html#show">show</a>();
|
---|
275 |
|
---|
276 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
277 | }
|
---|
278 | </pre>
|
---|
279 |
|
---|
280 | <p>See also <a href="examples.html">Examples</a>.
|
---|
281 |
|
---|
282 | <!-- eof -->
|
---|
283 | <p><address><hr><div align=center>
|
---|
284 | <table width=100% cellspacing=0 border=0><tr>
|
---|
285 | <td>Copyright © 2007
|
---|
286 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
287 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
288 | </table></div></address></body>
|
---|
289 | </html>
|
---|