1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/tutorial.qdoc -->
7
<title>Qt 4.0: Qt Tutorial 11 - Giving It a Shot</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
14
<link rel="prev" href="tutorial-t10.html" />
15
<link rel="contents" href="tutorial.html" />
16
<link rel="next" href="tutorial-t12.html" />
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
22
<td width="1"> </td><td class="postheader" 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>
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
24
[Previous: <a href="tutorial-t10.html">Chapter 10</a>]
25
[<a href="tutorial.html">Qt Tutorial</a>]
26
[Next: <a href="tutorial-t12.html">Chapter 12</a>]
28
<h1 align="center">Qt Tutorial 11 - Giving It a Shot</h1>
31
<li><a href="tutorial-t11-cannonfield-cpp.html">tutorial/t11/cannonfield.cpp</a></li>
32
<li><a href="tutorial-t11-cannonfield-h.html">tutorial/t11/cannonfield.h</a></li>
33
<li><a href="tutorial-t11-lcdrange-cpp.html">tutorial/t11/lcdrange.cpp</a></li>
34
<li><a href="tutorial-t11-lcdrange-h.html">tutorial/t11/lcdrange.h</a></li>
35
<li><a href="tutorial-t11-main-cpp.html">tutorial/t11/main.cpp</a></li>
37
<center><img src="images/t11.png" alt="Screenshot of tutorial eleven" /></center><p>In this example we introduce a timer to implement animated shooting.</p>
38
<a name="line-by-line-walkthrough"></a>
39
<h2>Line by Line Walkthrough</h2>
40
<a name="t11-cannonfield-h"></a>
41
<h3><a href="tutorial-t11-cannonfield-h.html">t11/cannonfield.h</a></h3>
42
<p>The <tt>CannonField</tt> now has shooting capabilities.</p>
43
<pre> void shoot();</pre>
44
<p>Calling this slot will make the cannon shoot if a shot is not in the air.</p>
45
<pre> private slots:
46
void moveShot();</pre>
47
<p>This private slot is used to move the shot while it is in the air, using a <a href="qtimer.html">QTimer</a>.</p>
49
void paintShot(QPainter &painter);</pre>
50
<p>This private function paints the shot.</p>
51
<pre> QRect shotRect() const;</pre>
52
<p>This private function returns the shot's enclosing rectangle if one is in the air; otherwise the returned rectangle is undefined.</p>
53
<pre> int timerCount;
54
QTimer *autoShootTimer;
58
<p>These private variables contain information that describes the shot. The <tt>timerCount</tt> keeps track of the time passed since the shot was fired. The <tt>shootAngle</tt> is the cannon angle and <tt>shootForce</tt> is the cannon force when the shot was fired.</p>
59
<a name="t11-cannonfield-cpp"></a>
60
<h3><a href="tutorial-t11-cannonfield-cpp.html">t11/cannonfield.cpp</a></h3>
61
<pre> #include <math.h></pre>
62
<p>We include <tt><math.h></tt> because we need the <tt>sin()</tt> and <tt>cos()</tt> functions. (An alternative would be to include the more modern <tt><cmath></tt> header file. Unfortunately, some Unix platforms still don't support these properly.)</p>
63
<pre> CannonField::CannonField(QWidget *parent)
69
autoShootTimer = new QTimer(this);
70
connect(autoShootTimer, SIGNAL(timeout()), this, SLOT(moveShot()));
73
setPalette(QPalette(QColor(250, 250, 200)));
75
<p>We initialize our new private variables and connect the <a href="qtimer.html#timeout">QTimer::timeout</a>() signal to our <tt>moveShot()</tt> slot. We'll move the shot every time the timer times out.</p>
76
<pre> void CannonField::shoot()
78
if (autoShootTimer->isActive())
81
shootAngle = currentAngle;
82
shootForce = currentForce;
83
autoShootTimer->start(5);
85
<p>This function shoots a shot unless a shot is in the air. The <tt>timerCount</tt> is reset to zero. The <tt>shootAngle</tt> and <tt>shootForce</tt> variables are set to the current cannon angle and force. Finally, we start the timer.</p>
86
<pre> void CannonField::moveShot()
88
QRegion region = shotRect();
91
QRect shotR = shotRect();
93
if (shotR.x() > width() || shotR.y() > height()) {
94
autoShootTimer->stop();
96
region = region.unite(shotR);
100
<p><tt>moveShot()</tt> is the slot that moves the shot, called every 5 milliseconds when the <a href="qtimer.html">QTimer</a> fires.</p>
101
<p>Its tasks are to compute the new position, update the screen with the shot in the new position, and if necessary, stop the timer.</p>
102
<p>First we make a <a href="qregion.html">QRegion</a> that holds the old <tt>shotRect()</tt>. A <a href="qregion.html">QRegion</a> is capable of holding any sort of region, and we'll use it here to simplify the painting. <tt>shotRect()</tt> returns the rectangle where the shot is now. It is explained in detail later.</p>
103
<p>Then we increment the <tt>timerCount</tt>, which has the effect of moving the shot one step along its trajectory.</p>
104
<p>Next we fetch the new shot rectangle.</p>
105
<p>If the shot has moved beyond the right or bottom edge of the widget we stop the timer, or we add the new <tt>shotRect()</tt> to the <a href="qregion.html">QRegion</a>.</p>
106
<p>Finally, we repaint the <a href="qregion.html">QRegion</a>. This will send a single paint event for just the one or two rectangles that need updating.</p>
107
<pre> void CannonField::paintEvent(QPaintEvent * /* event */)
109
QPainter painter(this);
111
paintCannon(painter);
112
if (autoShootTimer->isActive())
115
<p>The paint event function has been simplified since the previous chapter. Most of the logic has been moved to the new <tt>paintShot()</tt> and <tt>paintCannon()</tt> functions.</p>
116
<pre> void CannonField::paintShot(QPainter &painter)
118
painter.setPen(Qt::NoPen);
119
painter.setBrush(Qt::black);
120
painter.drawRect(shotRect());
122
<p>This private function paints the shot by drawing a black filled rectangle.</p>
123
<p>We leave out the implementation of <tt>paintCannon()</tt>; it is the same as the <a href="qwidget.html#paintEvent">QWidget::paintEvent</a>() reimplementation from the previous chapter.</p>
124
<pre> QRect CannonField::shotRect() const
126
const double gravity = 4;
128
double time = timerCount / 40.0;
129
double velocity = shootForce;
130
double radians = shootAngle * 3.14159265 / 180;
132
double velx = velocity * cos(radians);
133
double vely = velocity * sin(radians);
134
double x0 = (barrelRect.right() + 5) * cos(radians);
135
double y0 = (barrelRect.right() + 5) * sin(radians);
136
double x = x0 + velx * time;
137
double y = y0 + vely * time - 0.5 * gravity * time * time;
139
QRect result(0, 0, 6, 6);
140
result.moveCenter(QPoint(qRound(x), height() - 1 - qRound(y)));
143
<p>This private function calculates the center point of the shot and returns the enclosing rectangle of the shot. It uses the initial cannon force and angle in addition to <tt>timerCount</tt>, which increases as time passes.</p>
144
<p>The formula used is the standard Newtonian formula for frictionless movement in a gravity field. For simplicity, we've chosen to disregard any Einsteinian effects.</p>
145
<p>We calculate the center point in a coordinate system where <i>y</i> coordinates increase upward. After we have calculated the center point, we construct a <a href="qrect.html">QRect</a> with size 6 x 6 and move its center point to the point calculated above. In the same operation we convert the point into the widget's coordinate system (see <a href="coordsys.html">The Coordinate System</a>).</p>
146
<p>The <a href="qtglobal.html#qRound">qRound</a>() function is an inline function defined in <tt><QtGlobal></tt> (included by all other Qt header files). <a href="qtglobal.html#qRound">qRound</a>() rounds a double to the closest integer.</p>
147
<a name="t11-main-cpp"></a>
148
<h3><a href="tutorial-t11-main-cpp.html">t11/main.cpp</a></h3>
149
<pre> class MyWidget : public QWidget
152
MyWidget(QWidget *parent = 0);
154
<p>The only addition is the <b>Shoot</b> button.</p>
155
<pre> QPushButton *shoot = new QPushButton("&Shoot");
156
shoot->setFont(QFont("Times", 18, QFont::Bold));</pre>
157
<p>In the constructor we create and set up the <b>Shoot</b> button exactly like we did with the <b>Quit</b> button.</p>
158
<pre> connect(shoot, SIGNAL(clicked()), cannonField, SLOT(shoot()));</pre>
159
<p>Connects the <a href="qabstractbutton.html#clicked">clicked()</a> signal of the <b>Shoot</b> button to the <tt>shoot()</tt> slot of the <tt>CannonField</tt>.</p>
160
<a name="running-the-application"></a>
161
<h2>Running the Application</h2>
162
<p>The cannon can shoot, but there's nothing to shoot at.</p>
163
<a name="exercises"></a>
165
<p>Make the shot a filled circle. [Hint: <a href="qpainter.html#drawEllipse">QPainter::drawEllipse</a>() may help.]</p>
166
<p>Change the color of the cannon when a shot is in the air.</p>
168
[Previous: <a href="tutorial-t10.html">Chapter 10</a>]
169
[<a href="tutorial.html">Qt Tutorial</a>]
170
[Next: <a href="tutorial-t12.html">Chapter 12</a>]
172
<p /><address><hr /><div align="center">
173
<table width="100%" cellspacing="0" border="0"><tr class="address">
174
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
175
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
176
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
177
</tr></table></div></address></body>