File: hello-main-cpp.html

package info (click to toggle)
qt-embedded 2.3.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 68,608 kB
  • ctags: 45,998
  • sloc: cpp: 276,654; ansic: 71,987; makefile: 29,074; sh: 12,305; yacc: 2,465; python: 1,863; perl: 481; lex: 480; xml: 68; lisp: 15
file content (218 lines) | stat: -rw-r--r-- 7,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Toolkit - hello/main.cpp example file
</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">

<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>

<h1 align=center>Hello, World</h1><br clear="all">

  This example brings up the words "Hello, World" moving up and down,
  and in different colors.

  <hr>

  Header file: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/hello/hello.h   2.3.2   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef HELLO_H
#define HELLO_H

#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;

class Hello : public QWidget
{
    Q_OBJECT
public:
    Hello( const char *text, QWidget *parent=0, const char *name=0 );
signals:
    void clicked();
protected:
    void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
    void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
private slots:
    void animate();
private:
    <a href="qstring.html">QString</a> t;
    int     b;
};

#endif
</pre>


  <hr>

  Implementation: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/hello/hello.cpp   2.3.2   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  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 "hello.h"
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qtimer-h.html">qtimer.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;

/*
  Constructs a Hello widget. Starts a 40 ms animation timer.
*/

Hello::Hello( const char *text, QWidget *parent, const char *name )
    : <a href="qwidget.html">QWidget</a>(parent,name), t(text), b(0)
{
    <a href="qtimer.html">QTimer</a> *timer = new <a href="qtimer.html">QTimer</a>(this);
    <a href="qobject.html#7f8e37">connect</a>( timer, SIGNAL(timeout()), SLOT(<a href=#371>animate</a>()) );
    timer-&gt;<a href="qtimer.html#ce33bc">start</a>( 40 );

    <a href="qwidget.html#8fcbbe">resize</a>( 260, 130 );
}

/*
  This private slot is called each time the timer fires.
*/

void <a name="371"></a>Hello::animate()
{
    b = (b + 1) &amp; 15;
    <a href="qwidget.html#7569b1">repaint</a>( FALSE );
}

/*
  Handles mouse button release events for the Hello widget.

  We emit the clicked() signal when the mouse is released inside
  the widget.
*/

void <a name="372"></a>Hello::mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
    if ( rect().contains( e-&gt;<a href="qmouseevent.html#ac6f25">pos</a>() ) )
        emit clicked();
}

/*
  Handles paint events for the Hello widget.

  Flicker-free update. The text is first drawn in the pixmap and the
  pixmap is then blt'ed to the screen.
*/

void <a name="373"></a>Hello::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * )
{
    static int sin_tbl[16] = {
        0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38};

    if ( t.isEmpty() )
        return;

    // 1: Compute some sizes, positions etc.
    <a href="qfontmetrics.html">QFontMetrics</a> fm = fontMetrics();
    int w = fm.<a href="qfontmetrics.html#3b6f39">width</a>(t) + 20;
    int h = fm.<a href="qfontmetrics.html#341253">height</a>() * 2;
    int pmx = width()/2 - w/2;
    int pmy = height()/2 - h/2;

    // 2: Create the pixmap and fill it with the widget's background
    <a href="qpixmap.html">QPixmap</a> pm( w, h );
    pm.<a href="qpixmap.html#6910a0">fill</a>( this, pmx, pmy );

    // 3: Paint the pixmap. Cool wave effect
    <a href="qpainter.html">QPainter</a> p;
    int x = 10;
    int y = h/2 + fm.<a href="qfontmetrics.html#200b74">descent</a>();
    int i = 0;
    p.<a href="qpainter.html#02ed5d">begin</a>( &amp;pm );
    p.<a href="qpainter.html#998df2">setFont</a>( <a href="qwidget.html#167922">font</a>() );
    while ( !t[i].isNull() ) {
        int i16 = (b+i) &amp; 15;
        p.<a href="qpainter.html#0183e4">setPen</a>( <a href="qcolor.html">QColor</a>((15-i16)*16,255,255,QColor::Hsv) );
        p.<a href="qpainter.html#0f088f">drawText</a>( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 );
        x += fm.<a href="qfontmetrics.html#3b6f39">width</a>( t[i] );
        i++;
    }
    p.<a href="qpainter.html#365784">end</a>();

    // 4: Copy the pixmap to the Hello widget
    <a href="qpaintdevice.html#35ae2e">bitBlt</a>( this, pmx, pmy, &amp;pm );
}
</pre>


  <hr>

  Main:
<pre>/****************************************************************************
** &#36;Id&#58; qt/examples/hello/main.cpp   2.3.2   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  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 "hello.h"
#include &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;

/*
  The program starts here. It parses the command line and builds a message
  string to be displayed by the Hello widget.
*/

int main( int argc, char **argv )
{
    <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a(argc,argv);
    <a name="QString"></a><a href="qstring.html">QString</a> s;
    for ( int i=1; i&lt;argc; i++ ) {
        s += argv[i];
        if ( i&lt;argc-1 )
            s += " ";
    }
    if ( s.<a name="isEmpty"></a><a href="qstring.html#c62623">isEmpty</a>() )
        s = "Hello, World";
    Hello h( s );
    h.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt says hello" );
    <a name="QObject::connect"></a><a href="qobject.html#7f8e37">QObject::connect</a>( &amp;h, SIGNAL(clicked()), &amp;a, SLOT(quit()) );
    h.<a name="setFont"></a><a href="qwidget.html#090d60">setFont</a>( <a name="QFont"></a><a href="qfont.html">QFont</a>("times",32,QFont::Bold) );         // default font
    h.<a name="setBackgroundColor"></a><a href="qwidget.html#c09181">setBackgroundColor</a>( Qt::white );                  // default bg color
    a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &amp;h );
    h.<a name="show"></a><a href="qwidget.html#200ee5">show</a>();
    return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright  2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>