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/hello/hello.doc:5 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Hello, World</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>Hello, World</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example brings up the words "Hello, World" moving up and down,
|
---|
37 | and in different colors.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: hello-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
44 | **
|
---|
45 | ** This file is part of an example program for Qt. This example
|
---|
46 | ** program may be used, distributed and modified without limitation.
|
---|
47 | **
|
---|
48 | *****************************************************************************/
|
---|
49 |
|
---|
50 | #ifndef HELLO_H
|
---|
51 | #define HELLO_H
|
---|
52 |
|
---|
53 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
54 |
|
---|
55 |
|
---|
56 | class Hello : public <a href="qwidget.html">QWidget</a>
|
---|
57 | {
|
---|
58 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
59 | public:
|
---|
60 | Hello( const char *text, QWidget *parent=0, const char *name=0 );
|
---|
61 | signals:
|
---|
62 | void clicked();
|
---|
63 | protected:
|
---|
64 | void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
|
---|
65 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
|
---|
66 | private slots:
|
---|
67 | void animate();
|
---|
68 | private:
|
---|
69 | <a href="qstring.html">QString</a> t;
|
---|
70 | int b;
|
---|
71 | };
|
---|
72 |
|
---|
73 | #endif
|
---|
74 | </pre>
|
---|
75 |
|
---|
76 | <p> <hr>
|
---|
77 | <p> Implementation:
|
---|
78 | <p> <pre>/****************************************************************************
|
---|
79 | ** $Id: hello-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
80 | **
|
---|
81 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
82 | **
|
---|
83 | ** This file is part of an example program for Qt. This example
|
---|
84 | ** program may be used, distributed and modified without limitation.
|
---|
85 | **
|
---|
86 | *****************************************************************************/
|
---|
87 |
|
---|
88 | #include "hello.h"
|
---|
89 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
90 | #include <<a href="qtimer-h.html">qtimer.h</a>>
|
---|
91 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
92 | #include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
---|
93 |
|
---|
94 |
|
---|
95 | /*
|
---|
96 | Constructs a Hello widget. Starts a 40 ms animation timer.
|
---|
97 | */
|
---|
98 |
|
---|
99 | <a name="f473"></a>Hello::Hello( const char *text, QWidget *parent, const char *name )
|
---|
100 | : <a href="qwidget.html">QWidget</a>(parent,name), t(text), b(0)
|
---|
101 | {
|
---|
102 | <a href="qtimer.html">QTimer</a> *timer = new <a href="qtimer.html">QTimer</a>(this);
|
---|
103 | <a name="x1640"></a> <a href="qobject.html#connect">connect</a>( timer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), SLOT(animate()) );
|
---|
104 | <a name="x1639"></a> timer-><a href="qtimer.html#start">start</a>( 40 );
|
---|
105 |
|
---|
106 | <a href="qwidget.html#resize">resize</a>( 260, 130 );
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /*
|
---|
111 | This private slot is called each time the timer fires.
|
---|
112 | */
|
---|
113 |
|
---|
114 | void <a name="f474"></a>Hello::animate()
|
---|
115 | {
|
---|
116 | b = (b + 1) & 15;
|
---|
117 | <a href="qwidget.html#repaint">repaint</a>( FALSE );
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /*
|
---|
122 | Handles mouse button release events for the Hello widget.
|
---|
123 |
|
---|
124 | We emit the clicked() signal when the mouse is released inside
|
---|
125 | the widget.
|
---|
126 | */
|
---|
127 |
|
---|
128 | <a name="x1641"></a>void Hello::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
|
---|
129 | {
|
---|
130 | if ( <a href="qwidget.html#rect">rect</a>().contains( e-><a href="qmouseevent.html#pos">pos</a>() ) )
|
---|
131 | emit clicked();
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /*
|
---|
136 | Handles paint events for the Hello widget.
|
---|
137 |
|
---|
138 | Flicker-free update. The text is first drawn in the pixmap and the
|
---|
139 | pixmap is then blt'ed to the screen.
|
---|
140 | */
|
---|
141 |
|
---|
142 | void Hello::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
|
---|
143 | {
|
---|
144 | static int sin_tbl[16] = {
|
---|
145 | 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38};
|
---|
146 |
|
---|
147 | if ( t.isEmpty() )
|
---|
148 | return;
|
---|
149 |
|
---|
150 | // 1: Compute some sizes, positions etc.
|
---|
151 | <a href="qfontmetrics.html">QFontMetrics</a> fm = <a href="qwidget.html#fontMetrics">fontMetrics</a>();
|
---|
152 | <a name="x1631"></a> int w = fm.<a href="qfontmetrics.html#width">width</a>(t) + 20;
|
---|
153 | <a name="x1630"></a> int h = fm.<a href="qfontmetrics.html#height">height</a>() * 2;
|
---|
154 | int pmx = <a href="qwidget.html#width">width</a>()/2 - w/2;
|
---|
155 | int pmy = <a href="qwidget.html#height">height</a>()/2 - h/2;
|
---|
156 |
|
---|
157 | // 2: Create the pixmap and fill it with the widget's background
|
---|
158 | <a href="qpixmap.html">QPixmap</a> pm( w, h );
|
---|
159 | <a name="x1638"></a> pm.<a href="qpixmap.html#fill">fill</a>( this, pmx, pmy );
|
---|
160 |
|
---|
161 | // 3: Paint the pixmap. Cool wave effect
|
---|
162 | <a href="qpainter.html">QPainter</a> p;
|
---|
163 | int x = 10;
|
---|
164 | <a name="x1629"></a> int y = h/2 + fm.<a href="qfontmetrics.html#descent">descent</a>();
|
---|
165 | int i = 0;
|
---|
166 | <a name="x1633"></a> p.<a href="qpainter.html#begin">begin</a>( &pm );
|
---|
167 | <a name="x1636"></a> p.<a href="qpainter.html#setFont">setFont</a>( <a href="qwidget.html#font">font</a>() );
|
---|
168 | while ( !t[i].isNull() ) {
|
---|
169 | int i16 = (b+i) & 15;
|
---|
170 | p.<a href="qpainter.html#setPen">setPen</a>( QColor((15-i16)*16,255,255,QColor::Hsv) );
|
---|
171 | p.<a href="qpainter.html#drawText">drawText</a>( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 );
|
---|
172 | x += fm.<a href="qfontmetrics.html#width">width</a>( t[i] );
|
---|
173 | i++;
|
---|
174 | }
|
---|
175 | <a name="x1635"></a> p.<a href="qpainter.html#end">end</a>();
|
---|
176 |
|
---|
177 | // 4: Copy the pixmap to the Hello widget
|
---|
178 | <a href="qimage.html#bitBlt">bitBlt</a>( this, pmx, pmy, &pm );
|
---|
179 | }
|
---|
180 | </pre>
|
---|
181 |
|
---|
182 | <p> <hr>
|
---|
183 | <p> Main:
|
---|
184 | <p> <pre>/****************************************************************************
|
---|
185 | ** $Id: hello-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
186 | **
|
---|
187 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
188 | **
|
---|
189 | ** This file is part of an example program for Qt. This example
|
---|
190 | ** program may be used, distributed and modified without limitation.
|
---|
191 | **
|
---|
192 | *****************************************************************************/
|
---|
193 |
|
---|
194 | #include "hello.h"
|
---|
195 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
196 |
|
---|
197 |
|
---|
198 | /*
|
---|
199 | The program starts here. It parses the command line and builds a message
|
---|
200 | string to be displayed by the Hello widget.
|
---|
201 | */
|
---|
202 |
|
---|
203 | int main( int argc, char **argv )
|
---|
204 | {
|
---|
205 | <a href="qapplication.html">QApplication</a> a(argc,argv);
|
---|
206 | <a href="qstring.html">QString</a> s;
|
---|
207 | for ( int i=1; i<argc; i++ ) {
|
---|
208 | s += argv[i];
|
---|
209 | if ( i<argc-1 )
|
---|
210 | s += " ";
|
---|
211 | }
|
---|
212 | if ( s.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
213 | s = "Hello, World";
|
---|
214 | Hello h( s );
|
---|
215 | #ifndef QT_NO_WIDGET_TOPEXTRA // for Qt/Embedded minimal build
|
---|
216 | h.<a href="qwidget.html#setCaption">setCaption</a>( "Qt says hello" );
|
---|
217 | #endif
|
---|
218 | QObject::<a href="qobject.html#connect">connect</a>( &h, SIGNAL(clicked()), &a, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
219 | <a name="x1650"></a> h.<a href="qwidget.html#setFont">setFont</a>( QFont("times",32,QFont::Bold) ); // default font
|
---|
220 | <a name="x1648"></a> h.<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::white ); // default bg color
|
---|
221 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &h );
|
---|
222 | h.<a href="qwidget.html#show">show</a>();
|
---|
223 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
224 | }
|
---|
225 | </pre>
|
---|
226 |
|
---|
227 | <p>See also <a href="examples.html">Examples</a>.
|
---|
228 |
|
---|
229 | <!-- eof -->
|
---|
230 | <p><address><hr><div align=center>
|
---|
231 | <table width=100% cellspacing=0 border=0><tr>
|
---|
232 | <td>Copyright © 2007
|
---|
233 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
234 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
235 | </table></div></address></body>
|
---|
236 | </html>
|
---|