?? tutorial1-11.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-win-commercial-3.0.5/doc/tutorial.doc:1506 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Translator" content="Cavendish">
<meta name="Qt zh_CN Documents Website" content="http://www.qiliang.net/qt">
<title>Qt教程一 —— 第十一章:給它一個炮彈</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: #ffffff; color: black; font-family: "Times New Roman" }
--></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">主頁</font></a>
| <a href="classes.html">
<font color="#004faf">所有的類</font></a>
| <a href="mainclasses.html">
<font color="#004faf">主要的類</font></a>
| <a href="annotated.html">
<font color="#004faf">注釋的類</font></a>
| <a href="groups.html">
<font color="#004faf">分組的類</font></a>
| <a href="functions.html">
<font color="#004faf">函數(shù)</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教程一 —— 第十一章:給它一個炮彈</h1>
<p> <center><img src="t11.png" alt="Screenshot of tutorial eleven"></center>
<p> 在這個例子里我們介紹了一個定時器來實現(xiàn)動畫的射擊。
<p> <ul>
<li> <a href="t11-lcdrange-h.html">t11/lcdrange.h</a>包含LCDRange類定義。
<li> <a href="t11-lcdrange-cpp.html">t11/lcdrange.cpp</a>包含LCDRange類實現(xiàn)。
<li> <a href="t11-cannon-h.html">t11/cannon.h</a>包含CannonField類定義。
<li> <a href="t11-cannon-cpp.html">t11/cannon.cpp</a>包含CannonField類實現(xiàn)。
<li> <a href="t11-main-cpp.html">t11/main.cpp</a>包含MyWidget和main。
</ul>
<p> <h2> 一行一行地解說
</h2>
<a name="1"></a><p> <h3> <a href="t11-cannon-h.html">t11/cannon.h</a>
</h3>
<a name="1-1"></a><p> CannonField現(xiàn)在就有了射擊能力。
<p>
<p> <pre> void shoot();
</pre>
<p> 當(dāng)炮彈不在空中中,調(diào)用這個槽就會使加農(nóng)炮射擊。
<p> <pre> private slots:
void moveShot();
</pre>
<p> 當(dāng)炮彈正在空中時,這個私有槽使用一個<a href="qtimer.html">定時器</a>來移動射擊。
<p> <pre> private:
void paintShot( <a href="qpainter.html">QPainter</a> * );
</pre>
<p> 這個函數(shù)來畫射擊。
<p> <pre> <a href="qrect.html">QRect</a> shotRect() const;
</pre>
<p> 當(dāng)炮彈正在空中的時候,這個私有函數(shù)返回封裝它所占用空間的矩形,否則它就返回一個沒有定義的矩形。
<p> <pre> int timerCount;
<a href="qtimer.html">QTimer</a> * autoShootTimer;
float shoot_ang;
float shoot_f;
};
</pre>
<p> 這些私有變量包含了描述射擊的信息。<tt>timerCount</tt>保留了射擊進(jìn)行后的時間。<tt>shoot_ang</tt>是加農(nóng)炮射擊時的角度,<tt>shoot_f</tt>是射擊時加農(nóng)炮的力量。
<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> 我們包含了數(shù)學(xué)庫,因為我們需要使用sin()和cos()函數(shù)。
<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="x2311"></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> 我們初始化我們新的私有變量并且把<a href="qtimer.html#timeout">QTimer::timeout</a>()信號和我們的moveShot()槽相連。我們會在定時器超時的時候移動射擊。
<p> <pre> void CannonField::shoot()
{
<a name="x2308"></a> if ( autoShootTimer-><a href="qtimer.html#isActive">isActive</a>() )
return;
timerCount = 0;
shoot_ang = ang;
shoot_f = f;
<a name="x2309"></a> autoShootTimer-><a href="qtimer.html#start">start</a>( 50 );
}
</pre>
<p> 只要炮彈不在空中,這個函數(shù)就會進(jìn)行一次射擊。<tt>timerCount</tt>被重新設(shè)置為零。<tt>shoot_ang</tt>和<tt>shoot_f</tt>設(shè)置為當(dāng)前加農(nóng)炮的角度和力量。最后,我們開始這個定時器。
<p> <pre> void CannonField::moveShot()
{
<a href="qregion.html">QRegion</a> r( shotRect() );
timerCount++;
<a href="qrect.html">QRect</a> shotR = shotRect();
<a name="x2307"></a> if ( shotR.<a href="qrect.html#x">x</a>() > width() || shotR.<a href="qrect.html#y">y</a>() > height() )
<a name="x2310"></a> autoShootTimer-><a href="qtimer.html#stop">stop</a>();
else
<a name="x2305"></a> r = r.<a href="qrect.html#unite">unite</a>( QRegion( shotR ) );
<a href="qwidget.html#repaint">repaint</a>( r );
}
</pre>
<p> moveShot()是一個移動射擊的槽,當(dāng)<a href="qtimer.html">QTimer</a>開始的時候,每50毫秒被調(diào)用一次。
<p> 它的任務(wù)就是計算新的位置,重新畫屏幕并把炮彈放到新的位置,并且如果需要的話,停止定時器。
<p> 首先我們使用<a href="qregion.html">QRegion</a>來保留舊的shotRect()。<a href="qregion.html">QRegion</a>可以保留任何種類的區(qū)域,并且我們可以用它來簡化繪畫過程。shotRect()返回現(xiàn)在炮彈所在的矩形——稍后我們會詳細(xì)介紹。
<p> 然后我們增加<tt>timerCount</tt>,用它來實現(xiàn)炮彈在它的軌跡中移動的每一步。
<p> 下一步我們算出新的炮彈的矩形。
<p> 如果炮彈已經(jīng)移動到窗口部件的右面或者下面的邊界,我們停止定時器或者添加新的shotRect()到QRegion。
<p> 最后,我們重新繪制QRegion。這將會發(fā)送一個單一的繪畫事件,但僅僅有一個到兩個舉行需要刷新。
<p> <pre> void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
{
<a name="x2301"></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="x2302"></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> 繪畫事件函數(shù)在前一章中已經(jīng)被分成兩部分了。現(xiàn)在我們得到的新的矩形區(qū)域需要繪畫,檢查加農(nóng)炮和/或炮彈是否相交,并且如果需要的話,調(diào)用paintCannon()和/或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="x2298"></a> p-><a href="qpainter.html#drawRect">drawRect</a>( shotRect() );
}
</pre>
<p> 這個私有函數(shù)畫一個黑色填充的矩形作為炮彈。
<p> 我們把paintCannon()的實現(xiàn)放到一邊,它和前一章中的paintEvent()一樣。
<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="x2304"></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="x2303"></a> r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint( qRound(x), height() - 1 - qRound(y) ) );
return r;
}
</pre>
<p> 這個私有函數(shù)計算炮彈的中心點并且返回封裝炮彈的矩形。它除了使用自動增加所過去的時間的<tt>timerCount</tt>之外,還使用初始時的加農(nóng)炮的力量和角度。
<p> 運算公式使用的是有重力的環(huán)境下光滑運動的經(jīng)典牛頓公式。簡單地說,我們已經(jīng)選擇忽略愛因斯坦理論的結(jié)果。
<p> 我們在一個y坐標(biāo)向上增加的坐標(biāo)系統(tǒng)中計算中心點。在我們計算出中心點之后,我們構(gòu)造一個6*6大小的<a href="qrect.html">QRect</a>,并把它的中心移動到我們上面所計算出的中心點。同樣的操作我們把這個點移動到窗口部件的坐標(biāo)系統(tǒng)(請看<a href="coordsys.html">坐標(biāo)系統(tǒng)</a>)。
<p> qRound()函數(shù)是一個在qglobal.h中定義的內(nèi)嵌函數(shù)(被其它所有Qt頭文件包含)。qRound()把一個雙精度實數(shù)變?yōu)樽罱咏恼麛?shù)。
<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> 唯一的增加是Shoot按鈕。
<p> <pre> <a href="qpushbutton.html">QPushButton</a> *shoot = new <a href="qpushbutton.html">QPushButton</a>( "&Shoot", this, "shoot" );
shoot-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
</pre>
<p> 在構(gòu)造函數(shù)中我們創(chuàng)建和設(shè)置Shoot按鈕就像我們對Quit按鈕所做的那樣。注意構(gòu)造函數(shù)的第一個參數(shù)是按鈕的文本,并且第三個是窗口部件的名稱。
<p> <pre> <a href="qobject.html#connect">connect</a>( shoot, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), cannonField, SLOT(shoot()) );
</pre>
<p> 把Shoot按鈕的clicked()信號和CannonField的shoot()槽連接起來。
<p> <h2> 行為
</h2>
<a name="2"></a><p> The cannon can shoot, but there's nothing to shoot at.
<p> (請看<a href="tutorial1-07.html#compiling">編譯</a>來學(xué)習(xí)如何創(chuàng)建一個makefile和連編應(yīng)用程序。)
<p> <h2> 練習(xí)
</h2>
<a name="3"></a><p> 用一個填充的圓來表示炮彈。提示:<a href="qpainter.html#drawEllipse">QPainter::drawEllipse</a>()會對你有所幫助。
<p> 當(dāng)炮彈在空中的時候,改變加農(nóng)炮的顏色。
<p> 現(xiàn)在你可以進(jìn)行<a href="tutorial1-12.html">第十二章</a>了。
<p> [<a href="tutorial1-10.html">上一章</a>]
[<a href="tutorial1-12.html">下一章</a>]
[<a href="tutorial.html">教程一主頁</a>]
<p>
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright © 2002
<a href="http://www.trolltech.com">Trolltech</a>
<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td><a href="zh_CN.html">譯者:Cavendish</a>
<td align=right><div align=right>Qt 3.0.5版</div>
</table></div></address></body>
</html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -