~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t11.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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 -->
 
6
<head>
 
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" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 11 - Giving It a Shot</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
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>
 
36
</ul>
 
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>&nbsp;       void shoot();</pre>
 
44
<p>Calling this slot will make the cannon shoot if a shot is not in the air.</p>
 
45
<pre>&nbsp;   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>
 
48
<pre>&nbsp;   private:
 
49
        void paintShot(QPainter &amp;painter);</pre>
 
50
<p>This private function paints the shot.</p>
 
51
<pre>&nbsp;       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>&nbsp;       int timerCount;
 
54
        QTimer *autoShootTimer;
 
55
        float shootAngle;
 
56
        float shootForce;
 
57
    };</pre>
 
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>&nbsp;   #include &lt;math.h&gt;</pre>
 
62
<p>We include <tt>&lt;math.h&gt;</tt> because we need the <tt>sin()</tt> and <tt>cos()</tt> functions. (An alternative would be to include the more modern <tt>&lt;cmath&gt;</tt> header file. Unfortunately, some Unix platforms still don't support these properly.)</p>
 
63
<pre>&nbsp;   CannonField::CannonField(QWidget *parent)
 
64
        : QWidget(parent)
 
65
    {
 
66
        currentAngle = 45;
 
67
        currentForce = 0;
 
68
        timerCount = 0;
 
69
        autoShootTimer = new QTimer(this);
 
70
        connect(autoShootTimer, SIGNAL(timeout()), this, SLOT(moveShot()));
 
71
        shootAngle = 0;
 
72
        shootForce = 0;
 
73
        setPalette(QPalette(QColor(250, 250, 200)));
 
74
    }</pre>
 
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>&nbsp;   void CannonField::shoot()
 
77
    {
 
78
        if (autoShootTimer-&gt;isActive())
 
79
            return;
 
80
        timerCount = 0;
 
81
        shootAngle = currentAngle;
 
82
        shootForce = currentForce;
 
83
        autoShootTimer-&gt;start(5);
 
84
    }</pre>
 
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>&nbsp;   void CannonField::moveShot()
 
87
    {
 
88
        QRegion region = shotRect();
 
89
        ++timerCount;
 
90
 
 
91
        QRect shotR = shotRect();
 
92
 
 
93
        if (shotR.x() &gt; width() || shotR.y() &gt; height()) {
 
94
            autoShootTimer-&gt;stop();
 
95
        } else {
 
96
            region = region.unite(shotR);
 
97
        }
 
98
        update(region);
 
99
    }</pre>
 
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>&nbsp;   void CannonField::paintEvent(QPaintEvent * /* event */)
 
108
    {
 
109
        QPainter painter(this);
 
110
 
 
111
        paintCannon(painter);
 
112
        if (autoShootTimer-&gt;isActive())
 
113
            paintShot(painter);
 
114
    }</pre>
 
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>&nbsp;   void CannonField::paintShot(QPainter &amp;painter)
 
117
    {
 
118
        painter.setPen(Qt::NoPen);
 
119
        painter.setBrush(Qt::black);
 
120
        painter.drawRect(shotRect());
 
121
    }</pre>
 
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>&nbsp;   QRect CannonField::shotRect() const
 
125
    {
 
126
        const double gravity = 4;
 
127
 
 
128
        double time = timerCount / 40.0;
 
129
        double velocity = shootForce;
 
130
        double radians = shootAngle * 3.14159265 / 180;
 
131
 
 
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;
 
138
 
 
139
        QRect result(0, 0, 6, 6);
 
140
        result.moveCenter(QPoint(qRound(x), height() - 1 - qRound(y)));
 
141
        return result;
 
142
    }</pre>
 
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>&lt;QtGlobal&gt;</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>&nbsp;   class MyWidget : public QWidget
 
150
    {
 
151
    public:
 
152
        MyWidget(QWidget *parent = 0);
 
153
    };</pre>
 
154
<p>The only addition is the <b>Shoot</b> button.</p>
 
155
<pre>&nbsp;       QPushButton *shoot = new QPushButton(&quot;&amp;Shoot&quot;);
 
156
        shoot-&gt;setFont(QFont(&quot;Times&quot;, 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>&nbsp;       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>
 
164
<h2>Exercises</h2>
 
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>
 
167
<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>]
 
171
</p>
 
172
<p /><address><hr /><div align="center">
 
173
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
174
<td width="30%">Copyright &copy; 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>
 
178
</html>