You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
391 lines
15 KiB
391 lines
15 KiB
14 years ago
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||
|
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/progress/progress.doc:4 -->
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||
|
<title>Progress Bar and Dialog Example</title>
|
||
|
<style type="text/css"><!--
|
||
|
fn { margin-left: 1cm; text-indent: -1cm; }
|
||
|
a:link { color: #004faf; text-decoration: none }
|
||
|
a:visited { color: #672967; text-decoration: none }
|
||
|
body { background: #ffffff; color: black; }
|
||
|
--></style>
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
||
|
<tr bgcolor="#E5E5E5">
|
||
|
<td valign=center>
|
||
|
<a href="index.html">
|
||
|
<font color="#004faf">Home</font></a>
|
||
|
| <a href="classes.html">
|
||
|
<font color="#004faf">All Classes</font></a>
|
||
|
| <a href="mainclasses.html">
|
||
|
<font color="#004faf">Main Classes</font></a>
|
||
|
| <a href="annotated.html">
|
||
|
<font color="#004faf">Annotated</font></a>
|
||
|
| <a href="groups.html">
|
||
|
<font color="#004faf">Grouped Classes</font></a>
|
||
|
| <a href="functions.html">
|
||
|
<font color="#004faf">Functions</font></a>
|
||
|
</td>
|
||
|
<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>
|
||
|
|
||
|
|
||
|
<p>
|
||
|
This example displays either a simple (text-only) or a
|
||
|
custom-labelled (user-supplied widget) progress dialog. It also
|
||
|
demonstrates simple use of menus.
|
||
|
<hr>
|
||
|
<p> Implementation:
|
||
|
<p> <pre>/****************************************************************************
|
||
|
** $Id: qt/progress.cpp 3.3.8 edited Jan 11 14:37 $
|
||
|
**
|
||
|
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
||
|
**
|
||
|
** This file is part of an example program for Qt. This example
|
||
|
** program may be used, distributed and modified without limitation.
|
||
|
**
|
||
|
*****************************************************************************/
|
||
|
|
||
|
#include <<a href="qprogressdialog-h.html">qprogressdialog.h</a>>
|
||
|
#include <<a href="qapplication-h.html">qapplication.h</a>>
|
||
|
#include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
||
|
#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
||
|
#include <<a href="qpainter-h.html">qpainter.h</a>>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
class AnimatedThingy : public <a href="qlabel.html">QLabel</a> {
|
||
|
public:
|
||
|
AnimatedThingy( <a href="qwidget.html">QWidget</a>* parent, const <a href="qstring.html">QString</a>& s ) :
|
||
|
<a href="qlabel.html">QLabel</a>(parent),
|
||
|
label(s),
|
||
|
step(0)
|
||
|
{
|
||
|
setBackgroundColor(white);
|
||
|
label+="\n... and wasting CPU\nwith this animation!\n";
|
||
|
|
||
|
for (int i=0; i<nqix; i++)
|
||
|
ox[0][i] = oy[0][i] = ox[1][i] = oy[1][i] = 0;
|
||
|
x0 = y0 = x1 = y1 = 0;
|
||
|
dx0 = rand()%8+2;
|
||
|
dy0 = rand()%8+2;
|
||
|
dx1 = rand()%8+2;
|
||
|
dy1 = rand()%8+2;
|
||
|
}
|
||
|
|
||
|
void show()
|
||
|
{
|
||
|
if (!isVisible()) startTimer(100);
|
||
|
QWidget::<a href="qwidget.html#show">show</a>();
|
||
|
}
|
||
|
|
||
|
void hide()
|
||
|
{
|
||
|
<a name="x89"></a> QWidget::<a href="qwidget.html#hide">hide</a>();
|
||
|
killTimers();
|
||
|
}
|
||
|
|
||
|
<a href="qsize.html">QSize</a> sizeHint() const
|
||
|
{
|
||
|
return QSize(120,100);
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
void timerEvent(QTimerEvent*)
|
||
|
{
|
||
|
<a href="qpainter.html">QPainter</a> p(this);
|
||
|
<a name="x76"></a> <a href="qpen.html">QPen</a> pn=p.<a href="qpainter.html#pen">pen</a>();
|
||
|
<a name="x80"></a> pn.<a href="qpen.html#setWidth">setWidth</a>(2);
|
||
|
<a name="x79"></a> pn.<a href="qpen.html#setColor">setColor</a>(backgroundColor());
|
||
|
<a name="x78"></a> p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
||
|
|
||
|
step = (step + 1) % nqix;
|
||
|
|
||
|
<a name="x73"></a> p.<a href="qpainter.html#drawLine">drawLine</a>(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
|
||
|
|
||
|
inc(x0, dx0, width());
|
||
|
inc(y0, dy0, height());
|
||
|
inc(x1, dx1, width());
|
||
|
inc(y1, dy1, height());
|
||
|
ox[0][step] = x0;
|
||
|
oy[0][step] = y0;
|
||
|
ox[1][step] = x1;
|
||
|
oy[1][step] = y1;
|
||
|
|
||
|
<a href="qcolor.html">QColor</a> c;
|
||
|
<a name="x66"></a> c.<a href="qcolor.html#setHsv">setHsv</a>( (step*255)/nqix, 255, 255 ); // rainbow effect
|
||
|
pn.<a href="qpen.html#setColor">setColor</a>(c);
|
||
|
p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
||
|
p.<a href="qpainter.html#drawLine">drawLine</a>(ox[0][step], oy[0][step], ox[1][step], oy[1][step]);
|
||
|
p.<a href="qpainter.html#setPen">setPen</a>(colorGroup().text());
|
||
|
<a name="x74"></a> p.<a href="qpainter.html#drawText">drawText</a>(rect(), AlignCenter, label);
|
||
|
}
|
||
|
|
||
|
void paintEvent(QPaintEvent* event)
|
||
|
{
|
||
|
<a href="qpainter.html">QPainter</a> p(this);
|
||
|
<a href="qpen.html">QPen</a> pn=p.<a href="qpainter.html#pen">pen</a>();
|
||
|
pn.<a href="qpen.html#setWidth">setWidth</a>(2);
|
||
|
p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
||
|
<a name="x77"></a> p.<a href="qpainter.html#setClipRect">setClipRect</a>(event->rect());
|
||
|
for (int i=0; i<nqix; i++) {
|
||
|
<a href="qcolor.html">QColor</a> c;
|
||
|
c.<a href="qcolor.html#setHsv">setHsv</a>( (i*255)/nqix, 255, 255 ); // rainbow effect
|
||
|
pn.<a href="qpen.html#setColor">setColor</a>(c);
|
||
|
p.<a href="qpainter.html#setPen">setPen</a>(pn);
|
||
|
p.<a href="qpainter.html#drawLine">drawLine</a>(ox[0][i], oy[0][i], ox[1][i], oy[1][i]);
|
||
|
}
|
||
|
p.<a href="qpainter.html#setPen">setPen</a>(colorGroup().text());
|
||
|
p.<a href="qpainter.html#drawText">drawText</a>(rect(), AlignCenter, label);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
void inc(int& x, int& dx, int b)
|
||
|
{
|
||
|
x+=dx;
|
||
|
if (x<0) { x=0; dx=rand()%8+2; }
|
||
|
else if (x>=b) { x=b-1; dx=-(rand()%8+2); }
|
||
|
}
|
||
|
|
||
|
enum {nqix=10};
|
||
|
int ox[2][nqix];
|
||
|
int oy[2][nqix];
|
||
|
int x0,y0,x1,y1;
|
||
|
int dx0,dy0,dx1,dy1;
|
||
|
<a href="qstring.html">QString</a> label;
|
||
|
int step;
|
||
|
};
|
||
|
|
||
|
|
||
|
class CPUWaster : public <a href="qwidget.html">QWidget</a>
|
||
|
{
|
||
|
<a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
||
|
|
||
|
enum { first_draw_item = 1000, last_draw_item = 1006 };
|
||
|
|
||
|
int drawItemRects(int id)
|
||
|
{
|
||
|
int n = id - first_draw_item;
|
||
|
int r = 100;
|
||
|
while (n--) r*=(n%3 ? 5 : 4);
|
||
|
return r;
|
||
|
}
|
||
|
<a href="qstring.html">QString</a> drawItemText(int id)
|
||
|
{
|
||
|
<a href="qstring.html">QString</a> str;
|
||
|
str.<a href="qstring.html#sprintf">sprintf</a>("%d Rectangles", drawItemRects(id));
|
||
|
return str;
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
CPUWaster() :
|
||
|
pb(0)
|
||
|
{
|
||
|
menubar = new <a href="qmenubar.html">QMenuBar</a>( this, "menu" );
|
||
|
<a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( menubar );
|
||
|
|
||
|
<a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
||
|
<a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( file );
|
||
|
menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&File", file );
|
||
|
for (int i=first_draw_item; i<=last_draw_item; i++)
|
||
|
file-><a href="qmenudata.html#insertItem">insertItem</a>( drawItemText(i), i );
|
||
|
<a name="x67"></a> connect( menubar, SIGNAL(<a href="qmenubar.html#activated">activated</a>(int)), this, SLOT(doMenuItem(int)) );
|
||
|
<a name="x69"></a> file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
||
|
file-><a href="qmenudata.html#insertItem">insertItem</a>( "Quit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
||
|
|
||
|
options = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
||
|
<a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( options );
|
||
|
menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&Options", options );
|
||
|
td_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Timer driven", this, SLOT(timerDriven()) );
|
||
|
ld_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Loop driven", this, SLOT(loopDriven()) );
|
||
|
options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
||
|
dl_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Default label", this, SLOT(defaultLabel()) );
|
||
|
cl_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "Custom label", this, SLOT(customLabel()) );
|
||
|
options-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
||
|
md_id = options-><a href="qmenudata.html#insertItem">insertItem</a>( "No minimum duration", this, SLOT(toggleMinimumDuration()) );
|
||
|
<a name="x81"></a> options-><a href="qpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
|
||
|
loopDriven();
|
||
|
defaultLabel();
|
||
|
|
||
|
setFixedSize( 400, 300 );
|
||
|
|
||
|
setBackgroundColor( black );
|
||
|
}
|
||
|
|
||
|
public slots:
|
||
|
void doMenuItem(int id)
|
||
|
{
|
||
|
if (id >= first_draw_item && id <= last_draw_item)
|
||
|
draw(drawItemRects(id));
|
||
|
}
|
||
|
|
||
|
void stopDrawing() { got_stop = TRUE; }
|
||
|
|
||
|
void timerDriven()
|
||
|
{
|
||
|
timer_driven = TRUE;
|
||
|
<a name="x71"></a> options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( td_id, TRUE );
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ld_id, FALSE );
|
||
|
}
|
||
|
|
||
|
void loopDriven()
|
||
|
{
|
||
|
timer_driven = FALSE;
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( ld_id, TRUE );
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( td_id, FALSE );
|
||
|
}
|
||
|
|
||
|
void defaultLabel()
|
||
|
{
|
||
|
default_label = TRUE;
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( dl_id, TRUE );
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( cl_id, FALSE );
|
||
|
}
|
||
|
|
||
|
void customLabel()
|
||
|
{
|
||
|
default_label = FALSE;
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( dl_id, FALSE );
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( cl_id, TRUE );
|
||
|
}
|
||
|
|
||
|
void toggleMinimumDuration()
|
||
|
{
|
||
|
options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>( md_id,
|
||
|
<a name="x70"></a> !options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>( md_id ) );
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
void timerEvent( <a href="qtimerevent.html">QTimerEvent</a>* )
|
||
|
{
|
||
|
if (!got_stop)
|
||
|
<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 );
|
||
|
rects--;
|
||
|
|
||
|
{
|
||
|
<a href="qpainter.html">QPainter</a> p(this);
|
||
|
|
||
|
int ww = width();
|
||
|
int wh = height();
|
||
|
|
||
|
if ( ww > 8 && wh > 8 ) {
|
||
|
<a href="qcolor.html">QColor</a> c(rand()%255, rand()%255, rand()%255);
|
||
|
int x = rand() % (ww-8);
|
||
|
int y = rand() % (wh-8);
|
||
|
int w = rand() % (ww-x);
|
||
|
int h = rand() % (wh-y);
|
||
|
<a name="x75"></a> p.<a href="qpainter.html#fillRect">fillRect</a>( x, y, w, h, c );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!rects || got_stop) {
|
||
|
if (!got_stop)
|
||
|
pb-><a href="qprogressdialog.html#setProgress">setProgress</a>( pb-><a href="qprogressdialog.html#totalSteps">totalSteps</a>() );
|
||
|
<a href="qpainter.html">QPainter</a> p(this);
|
||
|
p.<a href="qpainter.html#fillRect">fillRect</a>(0, 0, width(), height(), backgroundColor());
|
||
|
enableDrawingItems(TRUE);
|
||
|
killTimers();
|
||
|
delete pb;
|
||
|
pb = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<a href="qprogressdialog.html">QProgressDialog</a>* newProgressDialog( const char* label, int steps, bool modal )
|
||
|
{
|
||
|
<a href="qprogressdialog.html">QProgressDialog</a> *d = new <a href="qprogressdialog.html">QProgressDialog</a>(label, "Cancel", steps, this,
|
||
|
"progress", modal);
|
||
|
if ( options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>( md_id ) )
|
||
|
<a name="x84"></a> d-><a href="qprogressdialog.html#setMinimumDuration">setMinimumDuration</a>(0);
|
||
|
if ( !default_label )
|
||
|
<a name="x83"></a> d-><a href="qprogressdialog.html#setLabel">setLabel</a>( new AnimatedThingy(d,label) );
|
||
|
return d;
|
||
|
}
|
||
|
|
||
|
void enableDrawingItems(bool yes)
|
||
|
{
|
||
|
for (int i=first_draw_item; i<=last_draw_item; i++) {
|
||
|
<a name="x72"></a> menubar-><a href="qmenudata.html#setItemEnabled">setItemEnabled</a>(i, yes);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void draw(int n)
|
||
|
{
|
||
|
if ( timer_driven ) {
|
||
|
if ( pb ) {
|
||
|
<a href="qapplication.html#qWarning">qWarning</a>("This cannot happen!");
|
||
|
return;
|
||
|
}
|
||
|
rects = n;
|
||
|
pb = newProgressDialog("Drawing rectangles.\n"
|
||
|
"Using timer event.", n, FALSE);
|
||
|
pb-><a href="qwidget.html#setCaption">setCaption</a>("Please Wait");
|
||
|
<a name="x82"></a> connect(pb, SIGNAL(<a href="qprogressdialog.html#cancelled">cancelled</a>()), this, SLOT(stopDrawing()));
|
||
|
enableDrawingItems(FALSE);
|
||
|
startTimer(0);
|
||
|
got_stop = FALSE;
|
||
|
} else {
|
||
|
<a href="qprogressdialog.html">QProgressDialog</a>* lpb = newProgressDialog(
|
||
|
"Drawing rectangles.\nUsing loop.", n, TRUE);
|
||
|
lpb-><a href="qwidget.html#setCaption">setCaption</a>("Please Wait");
|
||
|
|
||
|
<a href="qpainter.html">QPainter</a> p(this);
|
||
|
for (int i=0; i<n; i++) {
|
||
|
lpb-><a href="qprogressdialog.html#setProgress">setProgress</a>(i);
|
||
|
<a name="x87"></a> if ( lpb-><a href="qprogressdialog.html#wasCancelled">wasCancelled</a>() )
|
||
|
break;
|
||
|
|
||
|
<a href="qcolor.html">QColor</a> c(rand()%255, rand()%255, rand()%255);
|
||
|
int x = rand()%(width()-8);
|
||
|
int y = rand()%(height()-8);
|
||
|
int w = rand()%(width()-x);
|
||
|
int h = rand()%(height()-y);
|
||
|
p.<a href="qpainter.html#fillRect">fillRect</a>(x,y,w,h,c);
|
||
|
}
|
||
|
|
||
|
p.<a href="qpainter.html#fillRect">fillRect</a>(0, 0, width(), height(), backgroundColor());
|
||
|
|
||
|
delete lpb;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<a href="qmenubar.html">QMenuBar</a>* menubar;
|
||
|
<a href="qprogressdialog.html">QProgressDialog</a>* pb;
|
||
|
<a href="qpopupmenu.html">QPopupMenu</a>* options;
|
||
|
int td_id, ld_id;
|
||
|
int dl_id, cl_id;
|
||
|
int md_id;
|
||
|
int rects;
|
||
|
bool timer_driven;
|
||
|
bool default_label;
|
||
|
bool got_stop;
|
||
|
};
|
||
|
|
||
|
int main( int argc, char **argv )
|
||
|
{
|
||
|
<a href="qapplication.html">QApplication</a> a( argc, argv );
|
||
|
|
||
|
int wincount = argc > 1 ? atoi(argv[1]) : 1;
|
||
|
|
||
|
for ( int i=0; i<wincount; i++ ) {
|
||
|
CPUWaster* cpuw = new CPUWaster;
|
||
|
if ( i == 0 ) a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(cpuw);
|
||
|
cpuw-><a href="qwidget.html#show">show</a>();
|
||
|
}
|
||
|
return a.<a href="qapplication.html#exec">exec</a>();
|
||
|
}
|
||
|
|
||
|
#include "progress.moc"
|
||
|
</pre>
|
||
|
|
||
|
<p>See also <a href="examples.html">Examples</a>.
|
||
|
|
||
|
<!-- eof -->
|
||
|
<p><address><hr><div align=center>
|
||
|
<table width=100% cellspacing=0 border=0><tr>
|
||
|
<td>Copyright © 2007
|
||
|
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
||
|
<td align=right><div align=right>Qt 3.3.8</div>
|
||
|
</table></div></address></body>
|
||
|
</html>
|