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/progress/progress.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Progress Bar and Dialog Example</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 and Dialog Example</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example displays either a simple (text-only) or a
|
---|
37 | custom-labelled (user-supplied widget) progress dialog. It also
|
---|
38 | demonstrates simple use of menus.
|
---|
39 | <hr>
|
---|
40 | <p> Implementation:
|
---|
41 | <p> <pre>/****************************************************************************
|
---|
42 | ** $Id: progress-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
43 | **
|
---|
44 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
45 | **
|
---|
46 | ** This file is part of an example program for Qt. This example
|
---|
47 | ** program may be used, distributed and modified without limitation.
|
---|
48 | **
|
---|
49 | *****************************************************************************/
|
---|
50 |
|
---|
51 | #include <<a href="qprogressdialog-h.html">qprogressdialog.h</a>>
|
---|
52 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
53 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
54 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
55 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
56 | #include <stdlib.h>
|
---|
57 |
|
---|
58 | class AnimatedThingy : public <a href="qlabel.html">QLabel</a> {
|
---|
59 | public:
|
---|
60 | AnimatedThingy( <a href="qwidget.html">QWidget</a>* parent, const <a href="qstring.html">QString</a>& s ) :
|
---|
61 | <a href="qlabel.html">QLabel</a>(parent),
|
---|
62 | label(s),
|
---|
63 | step(0)
|
---|
64 | {
|
---|
65 | setBackgroundColor(white);
|
---|
66 | label+="\n... and wasting CPU\nwith this animation!\n";
|
---|
67 |
|
---|
68 | for (int i=0; i<nqix; i++)
|
---|
69 | ox[0][i] = oy[0][i] = ox[1][i] = oy[1][i] = 0;
|
---|
70 | x0 = y0 = x1 = y1 = 0;
|
---|
71 | dx0 = rand()%8+2;
|
---|
72 | dy0 = rand()%8+2;
|
---|
73 | dx1 = rand()%8+2;
|
---|
74 | dy1 = rand()%8+2;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void show()
|
---|
78 | {
|
---|
79 | if (!isVisible()) startTimer(100);
|
---|
80 | QWidget::<a href="qwidget.html#show">show</a>();
|
---|
81 | }
|
---|
82 |
|
---|
83 | void hide()
|
---|
84 | {
|
---|
85 | <a name="x89"></a> QWidget::<a href="qwidget.html#hide">hide</a>();
|
---|
86 | killTimers();
|
---|
87 | }
|
---|
88 |
|
---|
89 | <a href="qsize.html">QSize</a> sizeHint() const
|
---|
90 | {
|
---|
91 | return QSize(120,100);
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected:
|
---|
95 | void timerEvent(QTimerEvent*)
|
---|
96 | {
|
---|
97 | <a href="qpainter.html">QPainter</a> p(this);
|
---|
98 | <a name="x76"></a> <a href="qpen.html">QPen</a> pn=p.<a href="qpainter.html#pen">pen</a>();
|
---|
99 | <a name="x80"></a> pn.<a href="qpen.html#setWidth">setWidth</a>(2);
|
---|
100 | <a name="x79"></a> pn.<a href="qpen.html#setColor">setColor</a>(backgroundColor());
|
---|
101 | <a name="x78"></a> p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
---|
102 |
|
---|
103 | step = (step + 1) % nqix;
|
---|
104 |
|
---|
105 | <a name="x73"></a> p.<a href="qpainter.html#drawLine">drawLine</a>(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
|
---|
106 |
|
---|
107 | inc(x0, dx0, width());
|
---|
108 | inc(y0, dy0, height());
|
---|
109 | inc(x1, dx1, width());
|
---|
110 | inc(y1, dy1, height());
|
---|
111 | ox[0][step] = x0;
|
---|
112 | oy[0][step] = y0;
|
---|
113 | ox[1][step] = x1;
|
---|
114 | oy[1][step] = y1;
|
---|
115 |
|
---|
116 | <a href="qcolor.html">QColor</a> c;
|
---|
117 | <a name="x66"></a> c.<a href="qcolor.html#setHsv">setHsv</a>( (step*255)/nqix, 255, 255 ); // rainbow effect
|
---|
118 | pn.<a href="qpen.html#setColor">setColor</a>(c);
|
---|
119 | p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
---|
120 | p.<a href="qpainter.html#drawLine">drawLine</a>(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
|
---|
121 | p.<a href="qpainter.html#setPen">setPen</a>(colorGroup().text());
|
---|
122 | <a name="x74"></a> p.<a href="qpainter.html#drawText">drawText</a>(rect(), AlignCenter, label);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void paintEvent(QPaintEvent* event)
|
---|
126 | {
|
---|
127 | <a href="qpainter.html">QPainter</a> p(this);
|
---|
128 | <a href="qpen.html">QPen</a> pn=p.<a href="qpainter.html#pen">pen</a>();
|
---|
129 | pn.<a href="qpen.html#setWidth">setWidth</a>(2);
|
---|
130 | p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
---|
131 | <a name="x77"></a> p.<a href="qpainter.html#setClipRect">setClipRect</a>(event->rect());
|
---|
132 | for (int i=0; i<nqix; i++) {
|
---|
133 | <a href="qcolor.html">QColor</a> c;
|
---|
134 | c.<a href="qcolor.html#setHsv">setHsv</a>( (i*255)/nqix, 255, 255 ); // rainbow effect
|
---|
135 | pn.<a href="qpen.html#setColor">setColor</a>(c);
|
---|
136 | p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
---|
137 | p.<a href="qpainter.html#drawLine">drawLine</a>(ox[0][i], oy[0][i], ox[1][i], oy[1][i]);
|
---|
138 | }
|
---|
139 | p.<a href="qpainter.html#setPen">setPen</a>(colorGroup().text());
|
---|
140 | p.<a href="qpainter.html#drawText">drawText</a>(rect(), AlignCenter, label);
|
---|
141 | }
|
---|
142 |
|
---|
143 | private:
|
---|
144 | void inc(int& x, int& dx, int b)
|
---|
145 | {
|
---|
146 | x+=dx;
|
---|
147 | if (x<0) { x=0; dx=rand()%8+2; }
|
---|
148 | else if (x>=b) { x=b-1; dx=-(rand()%8+2); }
|
---|
149 | }
|
---|
150 |
|
---|
151 | enum {nqix=10};
|
---|
152 | int ox[2][nqix];
|
---|
153 | int oy[2][nqix];
|
---|
154 | int x0,y0,x1,y1;
|
---|
155 | int dx0,dy0,dx1,dy1;
|
---|
156 | <a href="qstring.html">QString</a> label;
|
---|
157 | int step;
|
---|
158 | };
|
---|
159 |
|
---|
160 |
|
---|
161 | class CPUWaster : public <a href="qwidget.html">QWidget</a>
|
---|
162 | {
|
---|
163 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
164 |
|
---|
165 | enum { first_draw_item = 1000, last_draw_item = 1006 };
|
---|
166 |
|
---|
167 | int drawItemRects(int id)
|
---|
168 | {
|
---|
169 | int n = id - first_draw_item;
|
---|
170 | int r = 100;
|
---|
171 | while (n--) r*=(n%3 ? 5 : 4);
|
---|
172 | return r;
|
---|
173 | }
|
---|
174 | <a href="qstring.html">QString</a> drawItemText(int id)
|
---|
175 | {
|
---|
176 | <a href="qstring.html">QString</a> str;
|
---|
177 | str.<a href="qstring.html#sprintf">sprintf</a>("%d Rectangles", drawItemRects(id));
|
---|
178 | return str;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public:
|
---|
182 | CPUWaster() :
|
---|
183 | pb(0)
|
---|
184 | {
|
---|
185 | menubar = new <a href="qmenubar.html">QMenuBar</a>( this, "menu" );
|
---|
186 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( menubar );
|
---|
187 |
|
---|
188 | <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
---|
189 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( file );
|
---|
190 | menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&File", file );
|
---|
191 | for (int i=first_draw_item; i<=last_draw_item; i++)
|
---|
192 | file-><a href="qmenudata.html#insertItem">insertItem</a>( drawItemText(i), i );
|
---|
193 | <a name="x67"></a> connect( menubar, SIGNAL(<a href="qmenubar.html#activated">activated</a>(int)), this, SLOT(doMenuItem(int)) );
|
---|
194 | <a name="x69"></a> file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
195 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Quit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
196 |
|
---|
197 | options = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
---|
198 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( options );
|
---|
199 | menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&Options", options );
|
---|
200 | td_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Timer driven", this, SLOT(timerDriven()) );
|
---|
201 | ld_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Loop driven", this, SLOT(loopDriven()) );
|
---|
202 | options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
203 | dl_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Default label", this, SLOT(defaultLabel()) );
|
---|
204 | cl_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Custom label", this, SLOT(customLabel()) );
|
---|
205 | options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
206 | md_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "No minimum duration", this, SLOT(toggleMinimumDuration()) );
|
---|
207 | <a name="x81"></a> options-><a href="qpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
|
---|
208 | loopDriven();
|
---|
209 | defaultLabel();
|
---|
210 |
|
---|
211 | setFixedSize( 400, 300 );
|
---|
212 |
|
---|
213 | setBackgroundColor( black );
|
---|
214 | }
|
---|
215 |
|
---|
216 | public slots:
|
---|
217 | void doMenuItem(int id)
|
---|
218 | {
|
---|
219 | if (id >= first_draw_item && id <= last_draw_item)
|
---|
220 | draw(drawItemRects(id));
|
---|
221 | }
|
---|
222 |
|
---|
223 | void stopDrawing() { got_stop = TRUE; }
|
---|
224 |
|
---|
225 | void timerDriven()
|
---|
226 | {
|
---|
227 | timer_driven = TRUE;
|
---|
228 | <a name="x71"></a> options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( td_id, TRUE );
|
---|
229 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ld_id, FALSE );
|
---|
230 | }
|
---|
231 |
|
---|
232 | void loopDriven()
|
---|
233 | {
|
---|
234 | timer_driven = FALSE;
|
---|
235 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ld_id, TRUE );
|
---|
236 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( td_id, FALSE );
|
---|
237 | }
|
---|
238 |
|
---|
239 | void defaultLabel()
|
---|
240 | {
|
---|
241 | default_label = TRUE;
|
---|
242 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( dl_id, TRUE );
|
---|
243 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( cl_id, FALSE );
|
---|
244 | }
|
---|
245 |
|
---|
246 | void customLabel()
|
---|
247 | {
|
---|
248 | default_label = FALSE;
|
---|
249 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( dl_id, FALSE );
|
---|
250 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( cl_id, TRUE );
|
---|
251 | }
|
---|
252 |
|
---|
253 | void toggleMinimumDuration()
|
---|
254 | {
|
---|
255 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( md_id,
|
---|
256 | <a name="x70"></a> !options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>( md_id ) );
|
---|
257 | }
|
---|
258 |
|
---|
259 | private:
|
---|
260 | void timerEvent( <a href="qtimerevent.html">QTimerEvent</a>* )
|
---|
261 | {
|
---|
262 | if (!got_stop)
|
---|
263 | <a name="x86"></a><a name="x85"></a> pb-><a href="qprogressdialog.html#setProgress">setProgress</a>( pb-><a href="qprogressdialog.html#totalSteps">totalSteps</a>() - rects );
|
---|
264 | rects--;
|
---|
265 |
|
---|
266 | {
|
---|
267 | <a href="qpainter.html">QPainter</a> p(this);
|
---|
268 |
|
---|
269 | int ww = width();
|
---|
270 | int wh = height();
|
---|
271 |
|
---|
272 | if ( ww > 8 && wh > 8 ) {
|
---|
273 | <a href="qcolor.html">QColor</a> c(rand()%255, rand()%255, rand()%255);
|
---|
274 | int x = rand() % (ww-8);
|
---|
275 | int y = rand() % (wh-8);
|
---|
276 | int w = rand() % (ww-x);
|
---|
277 | int h = rand() % (wh-y);
|
---|
278 | <a name="x75"></a> p.<a href="qpainter.html#fillRect">fillRect</a>( x, y, w, h, c );
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (!rects || got_stop) {
|
---|
283 | if (!got_stop)
|
---|
284 | pb-><a href="qprogressdialog.html#setProgress">setProgress</a>( pb-><a href="qprogressdialog.html#totalSteps">totalSteps</a>() );
|
---|
285 | <a href="qpainter.html">QPainter</a> p(this);
|
---|
286 | p.<a href="qpainter.html#fillRect">fillRect</a>(0, 0, width(), height(), backgroundColor());
|
---|
287 | enableDrawingItems(TRUE);
|
---|
288 | killTimers();
|
---|
289 | delete pb;
|
---|
290 | pb = 0;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | <a href="qprogressdialog.html">QProgressDialog</a>* newProgressDialog( const char* label, int steps, bool modal )
|
---|
295 | {
|
---|
296 | <a href="qprogressdialog.html">QProgressDialog</a> *d = new <a href="qprogressdialog.html">QProgressDialog</a>(label, "Cancel", steps, this,
|
---|
297 | "progress", modal);
|
---|
298 | if ( options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>( md_id ) )
|
---|
299 | <a name="x84"></a> d-><a href="qprogressdialog.html#setMinimumDuration">setMinimumDuration</a>(0);
|
---|
300 | if ( !default_label )
|
---|
301 | <a name="x83"></a> d-><a href="qprogressdialog.html#setLabel">setLabel</a>( new AnimatedThingy(d,label) );
|
---|
302 | return d;
|
---|
303 | }
|
---|
304 |
|
---|
305 | void enableDrawingItems(bool yes)
|
---|
306 | {
|
---|
307 | for (int i=first_draw_item; i<=last_draw_item; i++) {
|
---|
308 | <a name="x72"></a> menubar-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>(i, yes);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | void draw(int n)
|
---|
313 | {
|
---|
314 | if ( timer_driven ) {
|
---|
315 | if ( pb ) {
|
---|
316 | <a href="qapplication.html#qWarning">qWarning</a>("This cannot happen!");
|
---|
317 | return;
|
---|
318 | }
|
---|
319 | rects = n;
|
---|
320 | pb = newProgressDialog("Drawing rectangles.\n"
|
---|
321 | "Using timer event.", n, FALSE);
|
---|
322 | pb-><a href="qwidget.html#setCaption">setCaption</a>("Please Wait");
|
---|
323 | <a name="x82"></a> connect(pb, SIGNAL(<a href="qprogressdialog.html#cancelled">cancelled</a>()), this, SLOT(stopDrawing()));
|
---|
324 | enableDrawingItems(FALSE);
|
---|
325 | startTimer(0);
|
---|
326 | got_stop = FALSE;
|
---|
327 | } else {
|
---|
328 | <a href="qprogressdialog.html">QProgressDialog</a>* lpb = newProgressDialog(
|
---|
329 | "Drawing rectangles.\nUsing loop.", n, TRUE);
|
---|
330 | lpb-><a href="qwidget.html#setCaption">setCaption</a>("Please Wait");
|
---|
331 |
|
---|
332 | <a href="qpainter.html">QPainter</a> p(this);
|
---|
333 | for (int i=0; i<n; i++) {
|
---|
334 | lpb-><a href="qprogressdialog.html#setProgress">setProgress</a>(i);
|
---|
335 | <a name="x87"></a> if ( lpb-><a href="qprogressdialog.html#wasCancelled">wasCancelled</a>() )
|
---|
336 | break;
|
---|
337 |
|
---|
338 | <a href="qcolor.html">QColor</a> c(rand()%255, rand()%255, rand()%255);
|
---|
339 | int x = rand()%(width()-8);
|
---|
340 | int y = rand()%(height()-8);
|
---|
341 | int w = rand()%(width()-x);
|
---|
342 | int h = rand()%(height()-y);
|
---|
343 | p.<a href="qpainter.html#fillRect">fillRect</a>(x,y,w,h,c);
|
---|
344 | }
|
---|
345 |
|
---|
346 | p.<a href="qpainter.html#fillRect">fillRect</a>(0, 0, width(), height(), backgroundColor());
|
---|
347 |
|
---|
348 | delete lpb;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | <a href="qmenubar.html">QMenuBar</a>* menubar;
|
---|
353 | <a href="qprogressdialog.html">QProgressDialog</a>* pb;
|
---|
354 | <a href="qpopupmenu.html">QPopupMenu</a>* options;
|
---|
355 | int td_id, ld_id;
|
---|
356 | int dl_id, cl_id;
|
---|
357 | int md_id;
|
---|
358 | int rects;
|
---|
359 | bool timer_driven;
|
---|
360 | bool default_label;
|
---|
361 | bool got_stop;
|
---|
362 | };
|
---|
363 |
|
---|
364 | int main( int argc, char **argv )
|
---|
365 | {
|
---|
366 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
367 |
|
---|
368 | int wincount = argc > 1 ? atoi(argv[1]) : 1;
|
---|
369 |
|
---|
370 | for ( int i=0; i<wincount; i++ ) {
|
---|
371 | CPUWaster* cpuw = new CPUWaster;
|
---|
372 | if ( i == 0 ) a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(cpuw);
|
---|
373 | cpuw-><a href="qwidget.html#show">show</a>();
|
---|
374 | }
|
---|
375 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
376 | }
|
---|
377 |
|
---|
378 | #include "progress.moc"
|
---|
379 | </pre>
|
---|
380 |
|
---|
381 | <p>See also <a href="examples.html">Examples</a>.
|
---|
382 |
|
---|
383 | <!-- eof -->
|
---|
384 | <p><address><hr><div align=center>
|
---|
385 | <table width=100% cellspacing=0 border=0><tr>
|
---|
386 | <td>Copyright © 2007
|
---|
387 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
388 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
389 | </table></div></address></body>
|
---|
390 | </html>
|
---|