| 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/forever/forever.doc:5 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>A Rectangle Draw "Benchmark"</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>A Rectangle Draw "Benchmark"</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | <p> | 
|---|
| 36 | This example continuously draws rectangles in a window and | 
|---|
| 37 | has another widget that counts the number of rectangles that | 
|---|
| 38 | are drawn per second. | 
|---|
| 39 | <p> <hr> | 
|---|
| 40 | <p> Header file: | 
|---|
| 41 | <p> <pre>/**************************************************************************** | 
|---|
| 42 | ** $Id: forever-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 43 | ** | 
|---|
| 44 | ** Definition of something or other | 
|---|
| 45 | ** | 
|---|
| 46 | ** Created : 979899 | 
|---|
| 47 | ** | 
|---|
| 48 | ** Copyright (C) 1997-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 49 | ** | 
|---|
| 50 | ** This file is part of an example program for Qt.  This example | 
|---|
| 51 | ** program may be used, distributed and modified without limitation. | 
|---|
| 52 | ** | 
|---|
| 53 | *****************************************************************************/ | 
|---|
| 54 |  | 
|---|
| 55 | #ifndef FOREVER_H | 
|---|
| 56 | #define FOREVER_H | 
|---|
| 57 |  | 
|---|
| 58 | #include <<a href="qwidget-h.html">qwidget.h</a>> | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | const int numColors = 120; | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | class Forever : public <a href="qwidget.html">QWidget</a> | 
|---|
| 65 | { | 
|---|
| 66 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> | 
|---|
| 67 | public: | 
|---|
| 68 | Forever( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); | 
|---|
| 69 | protected: | 
|---|
| 70 | void        paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * ); | 
|---|
| 71 | void        timerEvent( <a href="qtimerevent.html">QTimerEvent</a> * ); | 
|---|
| 72 | private slots: | 
|---|
| 73 | void        updateCaption(); | 
|---|
| 74 | private: | 
|---|
| 75 | int         rectangles; | 
|---|
| 76 | QColor      colors[numColors]; | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | #endif | 
|---|
| 81 | </pre> | 
|---|
| 82 |  | 
|---|
| 83 | <p> <hr> | 
|---|
| 84 | <p> Implementation: | 
|---|
| 85 | <p> <pre>/**************************************************************************** | 
|---|
| 86 | ** $Id: forever-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 87 | ** | 
|---|
| 88 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 89 | ** | 
|---|
| 90 | ** This file is part of an example program for Qt.  This example | 
|---|
| 91 | ** program may be used, distributed and modified without limitation. | 
|---|
| 92 | ** | 
|---|
| 93 | *****************************************************************************/ | 
|---|
| 94 |  | 
|---|
| 95 | #include <<a href="qtimer-h.html">qtimer.h</a>> | 
|---|
| 96 | #include <<a href="qpainter-h.html">qpainter.h</a>> | 
|---|
| 97 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 98 | #include <stdlib.h>                             // defines rand() function | 
|---|
| 99 |  | 
|---|
| 100 | #include "forever.h" | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | // | 
|---|
| 104 | // Forever - a widget that draws rectangles forever. | 
|---|
| 105 | // | 
|---|
| 106 |  | 
|---|
| 107 | // | 
|---|
| 108 | // Constructs a Forever widget. | 
|---|
| 109 | // | 
|---|
| 110 |  | 
|---|
| 111 | <a name="f365"></a>Forever::Forever( <a href="qwidget.html">QWidget</a> *parent, const char *name ) | 
|---|
| 112 | : <a href="qwidget.html">QWidget</a>( parent, name ) | 
|---|
| 113 | { | 
|---|
| 114 | for (int a=0; a<numColors; a++) { | 
|---|
| 115 | colors[a] = QColor( rand()&255, | 
|---|
| 116 | rand()&255, | 
|---|
| 117 | rand()&255 ); | 
|---|
| 118 | } | 
|---|
| 119 | rectangles = 0; | 
|---|
| 120 | <a href="qobject.html#startTimer">startTimer</a>( 0 );                            // run continuous timer | 
|---|
| 121 | <a href="qtimer.html">QTimer</a> * counter = new <a href="qtimer.html">QTimer</a>( this ); | 
|---|
| 122 | <a name="x1054"></a>    <a href="qobject.html#connect">connect</a>( counter, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), | 
|---|
| 123 | this, SLOT(updateCaption()) ); | 
|---|
| 124 | <a name="x1053"></a>    counter-><a href="qtimer.html#start">start</a>( 1000 ); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | void <a name="f366"></a>Forever::updateCaption() | 
|---|
| 129 | { | 
|---|
| 130 | <a href="qstring.html">QString</a> s; | 
|---|
| 131 | <a name="x1052"></a>    s.<a href="qstring.html#sprintf">sprintf</a>( "Qt Example - Forever - %d rectangles/second", rectangles ); | 
|---|
| 132 | rectangles = 0; | 
|---|
| 133 | <a href="qwidget.html#setCaption">setCaption</a>( s ); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 | // | 
|---|
| 138 | // Handles paint events for the Forever widget. | 
|---|
| 139 | // | 
|---|
| 140 |  | 
|---|
| 141 | <a name="x1055"></a>void Forever::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ) | 
|---|
| 142 | { | 
|---|
| 143 | <a href="qpainter.html">QPainter</a> paint( this );                     // painter object | 
|---|
| 144 | int w = <a href="qwidget.html#width">width</a>(); | 
|---|
| 145 | int h = <a href="qwidget.html#height">height</a>(); | 
|---|
| 146 | if(w <= 0 || h <= 0) | 
|---|
| 147 | return; | 
|---|
| 148 | paint.<a href="qpainter.html#setPen">setPen</a>( NoPen );                      // do not draw outline | 
|---|
| 149 | paint.<a href="qpainter.html#setBrush">setBrush</a>( colors[rand() % numColors]);// set random brush color | 
|---|
| 150 |  | 
|---|
| 151 | <a href="qpoint.html">QPoint</a> p1( rand()%w, rand()%h );    // p1 = top left | 
|---|
| 152 | <a href="qpoint.html">QPoint</a> p2( rand()%w, rand()%h );    // p2 = bottom right | 
|---|
| 153 |  | 
|---|
| 154 | <a href="qrect.html">QRect</a> r( p1, p2 ); | 
|---|
| 155 | paint.<a href="qpainter.html#drawRect">drawRect</a>( r );                        // draw filled rectangle | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | // | 
|---|
| 159 | // Handles timer events for the Forever widget. | 
|---|
| 160 | // | 
|---|
| 161 |  | 
|---|
| 162 | <a name="x1048"></a>void Forever::<a href="qobject.html#timerEvent">timerEvent</a>( <a href="qtimerevent.html">QTimerEvent</a> * ) | 
|---|
| 163 | { | 
|---|
| 164 | for ( int i=0; i<100; i++ ) { | 
|---|
| 165 | <a href="qwidget.html#repaint">repaint</a>( FALSE );                       // repaint, don't erase | 
|---|
| 166 | rectangles++; | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 |  | 
|---|
| 171 | // | 
|---|
| 172 | // Create and display Forever widget. | 
|---|
| 173 | // | 
|---|
| 174 |  | 
|---|
| 175 | int main( int argc, char **argv ) | 
|---|
| 176 | { | 
|---|
| 177 | <a href="qapplication.html">QApplication</a> a( argc, argv );               // create application object | 
|---|
| 178 | Forever always;                             // create widget | 
|---|
| 179 | always.<a href="qwidget.html#resize">resize</a>( 400, 250 );                  // start up with size 400x250 | 
|---|
| 180 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &always );                 // set as main widget | 
|---|
| 181 | always.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Forever"); | 
|---|
| 182 | always.<a href="qwidget.html#show">show</a>();                              // show widget | 
|---|
| 183 | return a.<a href="qapplication.html#exec">exec</a>();                            // run event loop | 
|---|
| 184 | } | 
|---|
| 185 | </pre> | 
|---|
| 186 |  | 
|---|
| 187 | <p>See also <a href="examples.html">Examples</a>. | 
|---|
| 188 |  | 
|---|
| 189 | <!-- eof --> | 
|---|
| 190 | <p><address><hr><div align=center> | 
|---|
| 191 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 192 | <td>Copyright © 2007 | 
|---|
| 193 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 194 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 195 | </table></div></address></body> | 
|---|
| 196 | </html> | 
|---|