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

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t12.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 12 - Hanging in the Air the Way Bricks Don't</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-t11.html" />
 
15
    <link rel="contents" href="tutorial.html" />
 
16
    <link rel="next" href="tutorial-t13.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-t11.html">Chapter 11</a>]
 
25
[<a href="tutorial.html">Qt Tutorial</a>]
 
26
[Next: <a href="tutorial-t13.html">Chapter 13</a>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 12 - Hanging in the Air the Way Bricks Don't</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
31
<li><a href="tutorial-t12-cannonfield-cpp.html">tutorial/t12/cannonfield.cpp</a></li>
 
32
<li><a href="tutorial-t12-cannonfield-h.html">tutorial/t12/cannonfield.h</a></li>
 
33
<li><a href="tutorial-t12-lcdrange-cpp.html">tutorial/t12/lcdrange.cpp</a></li>
 
34
<li><a href="tutorial-t12-lcdrange-h.html">tutorial/t12/lcdrange.h</a></li>
 
35
<li><a href="tutorial-t12-main-cpp.html">tutorial/t12/main.cpp</a></li>
 
36
</ul>
 
37
<center><img src="images/t12.png" alt="Screenshot of tutorial twelve" /></center><p>In this example, we extend our <tt>LCDRange</tt> class to include a text label. We also provide something to shoot at.</p>
 
38
<a name="line-by-line-walkthrough"></a>
 
39
<h2>Line by Line Walkthrough</h2>
 
40
<a name="t12-lcdrange-h"></a>
 
41
<h3><a href="tutorial-t12-lcdrange-h.html">t12/lcdrange.h</a></h3>
 
42
<p>The <tt>LCDRange</tt> now has a text label.</p>
 
43
<pre>&nbsp;   class QLabel;
 
44
    class QSlider;</pre>
 
45
<p>We forward declare <a href="qlabel.html">QLabel</a> and <a href="qslider.html">QSlider</a> because we want to use pointers to them in the class definition. We could also use <tt>#include</tt>, but that would slow down compilation for nothing.</p>
 
46
<pre>&nbsp;   class LCDRange : public QWidget
 
47
    {
 
48
        Q_OBJECT
 
49
 
 
50
    public:
 
51
        LCDRange(QWidget *parent = 0);
 
52
        LCDRange(const QString &amp;text, QWidget *parent = 0);</pre>
 
53
<p>We have added a new constructor that sets the label text in addition to the parent.</p>
 
54
<pre>&nbsp;       QString text() const;</pre>
 
55
<p>This function returns the label text.</p>
 
56
<pre>&nbsp;       void setText(const QString &amp;text);</pre>
 
57
<p>This slot sets the label text.</p>
 
58
<pre>&nbsp;   private:
 
59
        void init();</pre>
 
60
<p>Because we now have two constructors, we have chosen to put the common initialization in the private <tt>init()</tt> function.</p>
 
61
<pre>&nbsp;       QLabel *label;</pre>
 
62
<p>We also have a new private variable: a <a href="qlabel.html">QLabel</a>. <a href="qlabel.html">QLabel</a> is one of Qt's standard widgets and can show a text or a <a href="qpixmap.html">QPixmap</a> with or without a frame.</p>
 
63
<a name="t12-lcdrange-cpp"></a>
 
64
<h3><a href="tutorial-t12-lcdrange-cpp.html">t12/lcdrange.cpp</a></h3>
 
65
<pre>&nbsp;   LCDRange::LCDRange(QWidget *parent)
 
66
        : QWidget(parent)
 
67
    {
 
68
        init();
 
69
    }</pre>
 
70
<p>This constructor calls the <tt>init()</tt> function, which contains the common initialization code.</p>
 
71
<pre>&nbsp;   LCDRange::LCDRange(const QString &amp;text, QWidget *parent)
 
72
        : QWidget(parent)
 
73
    {
 
74
        init();
 
75
        setText(text);
 
76
    }</pre>
 
77
<p>This constructor first calls <tt>init()</tt> and then sets the label text.</p>
 
78
<pre>&nbsp;   void LCDRange::init()
 
79
    {
 
80
        QLCDNumber *lcd = new QLCDNumber(2);
 
81
        slider = new QSlider(Qt::Horizontal);
 
82
        slider-&gt;setRange(0, 99);
 
83
        slider-&gt;setValue(0);
 
84
        label = new QLabel;
 
85
        label-&gt;setAlignment(Qt::AlignHCenter | Qt::AlignTop);
 
86
 
 
87
        connect(slider, SIGNAL(valueChanged(int)),
 
88
                lcd, SLOT(display(int)));
 
89
        connect(slider, SIGNAL(valueChanged(int)),
 
90
                this, SIGNAL(valueChanged(int)));
 
91
 
 
92
        QVBoxLayout *layout = new QVBoxLayout;
 
93
        layout-&gt;addWidget(lcd);
 
94
        layout-&gt;addWidget(slider);
 
95
        layout-&gt;addWidget(label);
 
96
        setLayout(layout);
 
97
 
 
98
        setFocusProxy(slider);
 
99
    }</pre>
 
100
<p>The setup of <tt>lcd</tt> and <tt>slider</tt> is the same as in the previous chapter. Next we create a <a href="qlabel.html">QLabel</a> and tell it to align the contents centered horizontally and to the top vertically. The <a href="qobject.html#connect">QObject::connect</a>() calls have also been taken from the previous chapter.</p>
 
101
<pre>&nbsp;   QString LCDRange::text() const
 
102
    {
 
103
        return label-&gt;text();
 
104
    }</pre>
 
105
<p>This function returns the label text.</p>
 
106
<pre>&nbsp;   void LCDRange::setText(const QString &amp;text)
 
107
    {
 
108
        label-&gt;setText(text);
 
109
    }</pre>
 
110
<p>This function sets the label text.</p>
 
111
<a name="t12-cannonfield-h"></a>
 
112
<h3><a href="tutorial-t12-cannonfield-h.html">t12/cannonfield.h</a></h3>
 
113
<p>The <tt>CannonField</tt> now has two new signals: <tt>hit()</tt> and <tt>missed()</tt>. In addition, it contains a target.</p>
 
114
<pre>&nbsp;       void newTarget();</pre>
 
115
<p>This slot creates a target at a new position.</p>
 
116
<pre>&nbsp;   signals:
 
117
        void hit();
 
118
        void missed();</pre>
 
119
<p>The <tt>hit()</tt> signal is emitted when a shot hits the target. The <tt>missed()</tt> signal is emitted when the shot moves beyond the right or bottom edge of the widget (i.e., it is certain that it has not and will not hit the target).</p>
 
120
<pre>&nbsp;       void paintTarget(QPainter &amp;painter);</pre>
 
121
<p>This private function paints the target.</p>
 
122
<pre>&nbsp;       QRect targetRect() const;</pre>
 
123
<p>This private function returns the enclosing rectangle of the target.</p>
 
124
<pre>&nbsp;       QPoint target;</pre>
 
125
<p>This private variable contains the center point of the target.</p>
 
126
<a name="t12-cannonfield-cpp"></a>
 
127
<h3><a href="tutorial-t12-cannonfield-cpp.html">t12/cannonfield.cpp</a></h3>
 
128
<pre>&nbsp;   #include &lt;stdlib.h&gt;</pre>
 
129
<p>We include the <tt>&lt;stdlib.h&gt;</tt> header file because we need the <tt>rand()</tt> function.</p>
 
130
<pre>&nbsp;       newTarget();</pre>
 
131
<p>This line has been added to the constructor. It creates a &quot;random&quot; position for the target. In fact, the <tt>newTarget()</tt> function will try to paint the target. Because we are in a constructor, the <tt>CannonField</tt> widget is invisible. Qt guarantees that no harm is done when calling <a href="qwidget.html#update">QWidget::update</a>() on a hidden widget.</p>
 
132
<pre>&nbsp;   void CannonField::newTarget()
 
133
    {
 
134
        static bool firstTime = true;
 
135
 
 
136
        if (firstTime) {
 
137
            firstTime = false;
 
138
            QTime midnight(0, 0, 0);
 
139
            srand(midnight.secsTo(QTime::currentTime()));
 
140
        }
 
141
        target = QPoint(200 + rand() % 190, 10 + rand() % 255);
 
142
        update();
 
143
    }</pre>
 
144
<p>This private function creates a target center point at a new random position.</p>
 
145
<p>We use the <tt>rand()</tt> function to fetch random integers. The <tt>rand()</tt> function normally returns the same series of numbers each time you run a program. This would make the target appear at the same position every time. To avoid this, we must set a random seed the first time this function is called. The random seed must also be random in order to avoid equal random number series. The solution is to use the number of seconds that have passed since midnight as a pseudo-random value.</p>
 
146
<p>First we create a static <tt>bool</tt> local variable. A static variable like this one is guaranteed to keep its value between calls to the function.</p>
 
147
<p>The <tt>if</tt> test will succeed only the first time this function is called because we set <tt>firstTime</tt> to <tt>false</tt> inside the <tt>if</tt> block.</p>
 
148
<p>Then we create the <a href="qtime.html">QTime</a> object <tt>midnight</tt>, which represents the time 00:00:00. Next we fetch the number of seconds from midnight until now and use it as a random seed. See the documentation for <a href="qdate.html">QDate</a>, <a href="qtime.html">QTime</a>, and <a href="qdatetime.html">QDateTime</a> for more information.</p>
 
149
<p>Finally we calculate the target's center point. We keep it within the rectangle (<i>x</i> = 200, <i>y</i> = 35, <i>width</i> = 190, <i>height</i> = 255), i.e., the possible <i>x</i> and <i>y</i> values are 200 to 389 and 35 to 289, respectively) in a coordinate system where we put <i>y</i> position 0 at the bottom edge of the widget and let <i>y</i> values increase upwards <i>x</i> is as normal, with 0 at the left edge and with <i>x</i> values increasing to the right.</p>
 
150
<p>By experimentation we have found this to always be in reach of the shot.</p>
 
151
<pre>&nbsp;   void CannonField::moveShot()
 
152
    {
 
153
        QRegion region = shotRect();
 
154
        ++timerCount;
 
155
 
 
156
        QRect shotR = shotRect();</pre>
 
157
<p>This part of the timer event has not changed from the previous chapter.</p>
 
158
<pre>&nbsp;       if (shotR.intersects(targetRect())) {
 
159
            autoShootTimer-&gt;stop();
 
160
            emit hit();</pre>
 
161
<p>This <tt>if</tt> statement checks whether the shot rectangle intersects the target rectangle. If it does, the shot has hit the target (ouch!). We stop the shoot timer and emit the <tt>hit()</tt> signal to tell the outside world that a target was destroyed, and return.</p>
 
162
<p>Note that we could have created a new target on the spot, but because the <tt>CannonField</tt> is a component we leave such decisions to the user of the component.</p>
 
163
<pre>&nbsp;       } else if (shotR.x() &gt; width() || shotR.y() &gt; height()) {
 
164
            autoShootTimer-&gt;stop();
 
165
            emit missed();</pre>
 
166
<p>This <tt>if</tt> statement is the same as in the previous chapter, except that it now emits the <tt>missed()</tt> signal to tell the outside world about the failure.</p>
 
167
<pre>&nbsp;       } else {</pre>
 
168
<p>And the rest of the function is as before.</p>
 
169
<p><tt>CannonField::paintEvent()</tt> is as before, except that this has been added:</p>
 
170
<pre>&nbsp;       paintTarget(painter);</pre>
 
171
<p>This line makes sure that the target is also painted when necessary.</p>
 
172
<pre>&nbsp;   void CannonField::paintTarget(QPainter &amp;painter)
 
173
    {
 
174
        painter.setPen(Qt::black);
 
175
        painter.setBrush(Qt::red);
 
176
        painter.drawRect(targetRect());
 
177
    }</pre>
 
178
<p>This private function paints the target; a rectangle filled with red and with a black outline.</p>
 
179
<pre>&nbsp;   QRect CannonField::targetRect() const
 
180
    {
 
181
        QRect result(0, 0, 20, 10);
 
182
        result.moveCenter(QPoint(target.x(), height() - 1 - target.y()));
 
183
        return result;
 
184
    }</pre>
 
185
<p>This private function returns the enclosing rectangle of the target. Remember from <tt>newTarget()</tt> that the <tt>target</tt> point uses <i>y</i> coordinate 0 at the bottom of the widget. We calculate the point in widget coordinates before we call <a href="qrect.html#moveCenter">QRect::moveCenter</a>().</p>
 
186
<p>The reason we have chosen this coordinate mapping is to fix the distance between the target and the bottom of the widget. Remember that the widget can be resized by the user or the program at any time.</p>
 
187
<a name="t12-main-cpp"></a>
 
188
<h3><a href="tutorial-t12-main-cpp.html">t12/main.cpp</a></h3>
 
189
<p>There are no new members in the <tt>MyWidget</tt> class, but we have slightly changed the constructor to set the new <tt>LCDRange</tt> text labels.</p>
 
190
<pre>&nbsp;       LCDRange *angle = new LCDRange(&quot;ANGLE&quot;);</pre>
 
191
<p>We set the angle text label to &quot;ANGLE&quot;.</p>
 
192
<pre>&nbsp;       LCDRange *force = new LCDRange(&quot;FORCE&quot;);</pre>
 
193
<p>We set the force text label to &quot;FORCE&quot;.</p>
 
194
<a name="running-the-application"></a>
 
195
<h2>Running the Application</h2>
 
196
<p>The <tt>LCDRange</tt> widgets look a bit strange: When resizing <tt>MyWidget</tt>, the built-in layout management in <a href="qvboxlayout.html">QVBoxLayout</a> gives the labels too much space and the rest not enough; making the space between the two <tt>LCDRange</tt> widgets change size. We'll fix that in the next chapter.</p>
 
197
<a name="exercises"></a>
 
198
<h2>Exercises</h2>
 
199
<p>Make a cheat button that, when pressed, makes the <tt>CannonField</tt> display the shot trajectory for five seconds.</p>
 
200
<p>If you did the &quot;round shot&quot; exercise from the previous chapter, try changing the <tt>shotRect()</tt> to a <tt>shotRegion()</tt> that returns a <a href="qregion.html">QRegion</a> so you can have really accurate collision detection.</p>
 
201
<p>Make a moving target.</p>
 
202
<p>Make sure that the target is always created entirely on-screen.</p>
 
203
<p>Make sure that the widget cannot be resized so that the target isn't visible. [Hint: <a href="qwidget.html#minimumSize-prop">QWidget::setMinimumSize</a>() is your friend.]</p>
 
204
<p>Not easy; make it possible to have several shots in the air at the same time. [Hint: Make a <tt>Shot</tt> class.]</p>
 
205
<p>
 
206
[Previous: <a href="tutorial-t11.html">Chapter 11</a>]
 
207
[<a href="tutorial.html">Qt Tutorial</a>]
 
208
[Next: <a href="tutorial-t13.html">Chapter 13</a>]
 
209
</p>
 
210
<p /><address><hr /><div align="center">
 
211
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
212
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
213
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
214
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
215
</tr></table></div></address></body>
 
216
</html>