1 | /****************************************************************************
|
---|
2 | ** $Id: forever.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include <qtimer.h>
|
---|
12 | #include <qpainter.h>
|
---|
13 | #include <qapplication.h>
|
---|
14 | #include <stdlib.h> // defines rand() function
|
---|
15 |
|
---|
16 | #include "forever.h"
|
---|
17 |
|
---|
18 |
|
---|
19 | //
|
---|
20 | // Forever - a widget that draws rectangles forever.
|
---|
21 | //
|
---|
22 |
|
---|
23 | //
|
---|
24 | // Constructs a Forever widget.
|
---|
25 | //
|
---|
26 |
|
---|
27 | Forever::Forever( QWidget *parent, const char *name )
|
---|
28 | : QWidget( parent, name )
|
---|
29 | {
|
---|
30 | for (int a=0; a<numColors; a++) {
|
---|
31 | colors[a] = QColor( rand()&255,
|
---|
32 | rand()&255,
|
---|
33 | rand()&255 );
|
---|
34 | }
|
---|
35 | rectangles = 0;
|
---|
36 | startTimer( 0 ); // run continuous timer
|
---|
37 | QTimer * counter = new QTimer( this );
|
---|
38 | connect( counter, SIGNAL(timeout()),
|
---|
39 | this, SLOT(updateCaption()) );
|
---|
40 | counter->start( 1000 );
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | void Forever::updateCaption()
|
---|
45 | {
|
---|
46 | QString s;
|
---|
47 | s.sprintf( "Qt Example - Forever - %d rectangles/second", rectangles );
|
---|
48 | rectangles = 0;
|
---|
49 | setCaption( s );
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | //
|
---|
54 | // Handles paint events for the Forever widget.
|
---|
55 | //
|
---|
56 |
|
---|
57 | void Forever::paintEvent( QPaintEvent * )
|
---|
58 | {
|
---|
59 | QPainter paint( this ); // painter object
|
---|
60 | int w = width();
|
---|
61 | int h = height();
|
---|
62 | if(w <= 0 || h <= 0)
|
---|
63 | return;
|
---|
64 | paint.setPen( NoPen ); // do not draw outline
|
---|
65 | paint.setBrush( colors[rand() % numColors]);// set random brush color
|
---|
66 |
|
---|
67 | QPoint p1( rand()%w, rand()%h ); // p1 = top left
|
---|
68 | QPoint p2( rand()%w, rand()%h ); // p2 = bottom right
|
---|
69 |
|
---|
70 | QRect r( p1, p2 );
|
---|
71 | paint.drawRect( r ); // draw filled rectangle
|
---|
72 | }
|
---|
73 |
|
---|
74 | //
|
---|
75 | // Handles timer events for the Forever widget.
|
---|
76 | //
|
---|
77 |
|
---|
78 | void Forever::timerEvent( QTimerEvent * )
|
---|
79 | {
|
---|
80 | for ( int i=0; i<100; i++ ) {
|
---|
81 | repaint( FALSE ); // repaint, don't erase
|
---|
82 | rectangles++;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | //
|
---|
88 | // Create and display Forever widget.
|
---|
89 | //
|
---|
90 |
|
---|
91 | int main( int argc, char **argv )
|
---|
92 | {
|
---|
93 | QApplication a( argc, argv ); // create application object
|
---|
94 | Forever always; // create widget
|
---|
95 | always.resize( 400, 250 ); // start up with size 400x250
|
---|
96 | a.setMainWidget( &always ); // set as main widget
|
---|
97 | always.setCaption("Qt Example - Forever");
|
---|
98 | always.show(); // show widget
|
---|
99 | return a.exec(); // run event loop
|
---|
100 | }
|
---|