diff options
Diffstat (limited to 'doc/html/tutorial1-11.html')
-rw-r--r-- | doc/html/tutorial1-11.html | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/doc/html/tutorial1-11.html b/doc/html/tutorial1-11.html new file mode 100644 index 0000000..0414de1 --- /dev/null +++ b/doc/html/tutorial1-11.html @@ -0,0 +1,250 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> +<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:1506 --> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> +<title>Qt Tutorial - Chapter 11: Giving It a Shot</title> +<style type="text/css"><!-- +fn { margin-left: 1cm; text-indent: -1cm; } +a:link { color: #004faf; text-decoration: none } +a:visited { color: #672967; text-decoration: none } +body { background: #ffffff; color: black; } +--></style> +</head> +<body> + +<table border="0" cellpadding="0" cellspacing="0" width="100%"> +<tr bgcolor="#E5E5E5"> +<td 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> +<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Qt Tutorial - Chapter 11: Giving It a Shot</h1> + + +<p> <center><img src="t11.png" alt="Screenshot of tutorial eleven"></center> +<p> In this example we introduce a timer to implement animated shooting. +<p> <ul> +<li> <a href="t11-lcdrange-h.html">t11/lcdrange.h</a> contains the LCDRange +class definition. +<li> <a href="t11-lcdrange-cpp.html">t11/lcdrange.cpp</a> contains the LCDRange +implementation. +<li> <a href="t11-cannon-h.html">t11/cannon.h</a> contains the CannonField class +definition. +<li> <a href="t11-cannon-cpp.html">t11/cannon.cpp</a> contains the CannonField +implementation. +<li> <a href="t11-main-cpp.html">t11/main.cpp</a> contains MyWidget and main. +</ul> +<p> <h2> Line-by-line Walkthrough +</h2> +<a name="1"></a><p> <h3> <a href="t11-cannon-h.html">t11/cannon.h</a> +</h3> +<a name="1-1"></a><p> The CannonField now has shooting capabilities. +<p> + +<p> <pre> void shoot(); +</pre> +<p> Calling this slot will make the cannon shoot if a shot is not in the air. +<p> <pre> private slots: + void moveShot(); +</pre> +<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> <pre> private: + void paintShot( <a href="qpainter.html">QPainter</a> * ); +</pre> +<p> This private function paints the shot. +<p> <pre> <a href="qrect.html">QRect</a> shotRect() const; +</pre> +<p> This private function returns the shot's enclosing rectangle if +one is in the air; otherwise the returned rectangle is undefined. +<p> <pre> int timerCount; + <a href="qtimer.html">QTimer</a> * autoShootTimer; + float shoot_ang; + float shoot_f; + }; +</pre> +<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>shoot_ang</tt> is the cannon angle and <tt>shoot_f</tt> is the cannon force +when the shot was fired. +<p> <h3> <a href="t11-cannon-cpp.html">t11/cannon.cpp</a> +</h3> +<a name="1-2"></a><p> + +<p> <pre> #include <math.h> +</pre> +<p> We include the math library because we need the sin() and cos() functions. +<p> <pre> CannonField::CannonField( <a href="qwidget.html">QWidget</a> *parent, const char *name ) + : <a href="qwidget.html">QWidget</a>( parent, name ) + { + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new <a href="qtimer.html">QTimer</a>( this, "movement handler" ); + <a name="x2379"></a> <a href="qobject.html#connect">connect</a>( autoShootTimer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + <a href="qwidget.html#setPalette">setPalette</a>( QPalette( QColor( 250, 250, 200) ) ); + } +</pre> +<p> We initialize our new private variables and connect the <a href="qtimer.html#timeout">QTimer::timeout</a>() signal to our moveShot() slot. We'll move the +shot every time the timer times out. +<p> <pre> void CannonField::shoot() + { + <a name="x2376"></a> if ( autoShootTimer-><a href="qtimer.html#isActive">isActive</a>() ) + return; + timerCount = 0; + shoot_ang = ang; + shoot_f = f; + <a name="x2377"></a> autoShootTimer-><a href="qtimer.html#start">start</a>( 50 ); + } +</pre> +<p> This function shoots a shot unless a shot is in the air. The <tt>timerCount</tt> +is reset to zero. The <tt>shoot_ang</tt> and <tt>shoot_f</tt> are set to the current +cannon angle and force. Finally, we start the timer. +<p> <pre> void CannonField::moveShot() + { + <a href="qregion.html">QRegion</a> r( shotRect() ); + timerCount++; + + <a href="qrect.html">QRect</a> shotR = shotRect(); + + if ( shotR.<a href="qrect.html#x">x</a>() > width() || shotR.<a href="qrect.html#y">y</a>() > height() ) + <a name="x2378"></a> autoShootTimer-><a href="qtimer.html#stop">stop</a>(); + else + <a name="x2373"></a> r = r.<a href="qrect.html#unite">unite</a>( QRegion( shotR ) ); + <a href="qwidget.html#repaint">repaint</a>( r ); + } +</pre> +<p> moveShot() is the slot that moves the shot, called every 50 +milliseconds when the <a href="qtimer.html">QTimer</a> fires. +<p> Its tasks are to compute the new position, repaint the screen with the +shot in the new position, and if necessary, stop the timer. +<p> First we make a <a href="qregion.html">QRegion</a> that holds the old shotRect(). 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. ShotRect() returns the rectangle where the +shot is now - it is explained in detail later. +<p> Then we increment the <tt>timerCount</tt>, which has the effect of moving the +shot one step along its trajectory. +<p> Next we fetch the new shot rectangle. +<p> If the shot has moved beyond the right or bottom edge of the widget, we +stop the timer or we add the new shotRect() to the QRegion. +<p> Finally, we repaint the QRegion. This will send a single paint event +for just the one or two rectangles that need updating. +<p> <pre> void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e ) + { + <a name="x2369"></a> <a href="qrect.html">QRect</a> updateR = e-><a href="qpaintevent.html#rect">rect</a>(); + <a href="qpainter.html">QPainter</a> p( this ); + + <a name="x2370"></a> if ( updateR.<a href="qrect.html#intersects">intersects</a>( cannonRect() ) ) + paintCannon( &p ); + if ( autoShootTimer-><a href="qtimer.html#isActive">isActive</a>() && + updateR.<a href="qrect.html#intersects">intersects</a>( shotRect() ) ) + paintShot( &p ); + } +</pre> +<p> The paint event function has been split in two since the previous +chapter. Now we fetch the bounding rectangle of the region that +needs painting, check whether it intersects either the cannon and/or +the shot, and if necessary, call paintCannon() and/or paintShot(). +<p> <pre> void CannonField::paintShot( <a href="qpainter.html">QPainter</a> *p ) + { + p-><a href="qpainter.html#setBrush">setBrush</a>( black ); + p-><a href="qpainter.html#setPen">setPen</a>( NoPen ); + <a name="x2366"></a> p-><a href="qpainter.html#drawRect">drawRect</a>( shotRect() ); + } +</pre> +<p> This private function paints the shot by drawing a black filled rectangle. +<p> We leave out the implementation of paintCannon(); it is the same as +the paintEvent() from the previous chapter. +<p> <pre> QRect CannonField::shotRect() const + { + const double gravity = 4; + + double time = timerCount / 4.0; + double velocity = shoot_f; + double radians = shoot_ang*3.14159265/180; + + double velx = velocity*cos( radians ); + double vely = velocity*sin( radians ); + <a name="x2372"></a> double x0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*cos(radians); + double y0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*sin(radians); + double x = x0 + velx*time; + double y = y0 + vely*time - 0.5*gravity*time*time; + + <a href="qrect.html">QRect</a> r = QRect( 0, 0, 6, 6 ); + <a name="x2371"></a> r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; + } +</pre> +<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> The formula used is the classical Newtonian formula for frictionless +movement in a gravity field. For simplicity, we've chosen to +disregard any Einsteinian effects. +<p> We calculate the center point in a coordinate system where y +coordinates increase upward. After we have calculated the center +point, we construct a <a href="qrect.html">QRect</a> with size 6x6 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> The qRound() function is an inline function defined in qglobal.h (included +by all other Qt header files). qRound() rounds a double to the closest +integer. +<p> <h3> <a href="t11-main-cpp.html">t11/main.cpp</a> +</h3> +<a name="1-3"></a><p> + +<p> <pre> class MyWidget: public <a href="qwidget.html">QWidget</a> + { + public: + MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); + }; +</pre> +<p> The only addition is the Shoot button. +<p> <pre> <a href="qpushbutton.html">QPushButton</a> *shoot = new <a href="qpushbutton.html">QPushButton</a>( "&Shoot", this, "shoot" ); + <a name="x2382"></a> shoot-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) ); +</pre> +<p> In the constructor we create and set up the Shoot button exactly like we +did with the Quit button. Note that the first argument to the constructor +is the button text, and the third is the widget's name. +<p> <pre> <a href="qobject.html#connect">connect</a>( shoot, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), cannonField, SLOT(shoot()) ); +</pre> +<p> Connects the clicked() signal of the Shoot button to the shoot() slot +of the CannonField. +<p> <h2> Behavior +</h2> +<a name="2"></a><p> The cannon can shoot, but there's nothing to shoot at. +<p> (See <a href="tutorial1-07.html#compiling">Compiling</a> for how to create a +makefile and build the application.) +<p> <h2> Exercises +</h2> +<a name="3"></a><p> Make the shot a filled circle. Hint: <a href="qpainter.html#drawEllipse">QPainter::drawEllipse</a>() may +help. +<p> Change the color of the cannon when a shot is in the air. +<p> You're now ready for <a href="tutorial1-12.html">Chapter 12.</a> +<p> [<a href="tutorial1-10.html">Previous tutorial</a>] +[<a href="tutorial1-12.html">Next tutorial</a>] +[<a href="tutorial.html">Main tutorial page</a>] +<p> +<!-- eof --> +<p><address><hr><div align=center> +<table width=100% cellspacing=0 border=0><tr> +<td>Copyright © 2007 +<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> +<td align=right><div align=right>Qt 3.3.8</div> +</table></div></address></body> +</html> |