?? tutorial1-10.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:1304 -->
<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">函數</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="t10.png" alt="Screenshot of tutorial ten"></center>
<p> 在這個例子中,我們介紹畫一個pixmap來除去閃爍。我們也會加入一個力量控制。
<p> <ul>
<li> <a href="t10-lcdrange-h.html">t10/lcdrange.h</a>包含LCDRange類定義。
<li> <a href="t10-lcdrange-cpp.html">t10/lcdrange.cpp</a>包含LCDRange類實現。
<li> <a href="t10-cannon-h.html">t10/cannon.h</a>包含CannonField類定義。
<li> <a href="t10-cannon-cpp.html">t10/cannon.cpp</a>包含CannonField類實現。
<li> <a href="t10-main-cpp.html">t10/main.cpp</a>包含MyWidget和main。
</ul>
<p> <h2> 一行一行地解說
</h2>
<a name="1"></a><p> <h3> <a href="t10-cannon-h.html">t10/cannon.h</a>
</h3>
<a name="1-1"></a><p> CannonField現在除了角度又多了一個力量值。
<p>
<p> <pre> int angle() const { return ang; }
int force() const { return f; }
public slots:
void setAngle( int degrees );
void setForce( int newton );
signals:
void angleChanged( int );
void forceChanged( int );
</pre>
<p> 力量的接口的實現和角度一樣。
<p> <pre> private:
<a href="qrect.html">QRect</a> cannonRect() const;
</pre>
<p> 我們把加農炮封裝的矩形的定義放到了一個單獨的函數中。
<p> <pre> int ang;
int f;
};
</pre>
<p> 力量被存儲到一個整數<tt>f</tt>中。
<p> <h3> <a href="t10-cannon-cpp.html">t10/cannon.cpp</a>
</h3>
<a name="1-2"></a><p>
<p> <pre> #include <<a href="qpixmap-h.html">qpixmap.h</a>>
</pre>
<p> 我們包含了<a href="qpixmap.html">QPixmap</a>類定義。
<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;
<a href="qwidget.html#setPalette">setPalette</a>( QPalette( QColor( 250, 250, 200) ) );
}
</pre>
<p> 力量(<tt>f</tt>)被初始化為0。
<p> <pre> void CannonField::setAngle( int degrees )
{
if ( degrees < 5 )
degrees = 5;
if ( degrees > 70 )
degrees = 70;
if ( ang == degrees )
return;
ang = degrees;
<a href="qwidget.html#repaint">repaint</a>( cannonRect(), FALSE );
emit angleChanged( ang );
}
</pre>
<p> 我們在setAngle()函數中做了一個小的改變。它只重畫窗口部件中含有加農炮的一小部分。FALSE參數說明在一個繪畫事件發送到窗口部件之前指定的矩形將不會被擦去。這將會使繪畫過程加速和平滑。
<p> <pre> void CannonField::setForce( int newton )
{
if ( newton < 0 )
newton = 0;
if ( f == newton )
return;
f = newton;
emit forceChanged( f );
}
</pre>
<p> setForce()的實現和setAngle()很相似。唯一的不同是因為我們不顯示力量值,我們不需要重畫窗口部件。
<p> <pre> void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
{
<a name="x2289"></a> if ( !e-><a href="qpaintevent.html#rect">rect</a>().intersects( cannonRect() ) )
return;
</pre>
<p> 我們現在用只重畫需要刷新得部分來優化繪畫事件。首先我們檢查是否不得不完全重畫任何事,我們返回是否不需要。
<p> <pre> <a href="qrect.html">QRect</a> cr = cannonRect();
<a name="x2293"></a> <a href="qpixmap.html">QPixmap</a> pix( cr.<a href="qrect.html#size">size</a>() );
</pre>
<p> 然后,我們創建一個臨時的pixmap,我們用來不閃爍地畫。所有的繪畫操作都在這個pixmap中完成,并且之后只用一步操作來把這個pixmap畫到屏幕上。
<p> 這是不閃爍繪畫的本質:一次準確地在每一個像素上畫。更少,你會得到繪畫錯誤。更多,你會得到閃爍。在這個例子中這個并不重要——當代碼被寫時,仍然是很慢的機器導致閃爍,但以后不會再閃爍了。我們由于教育目的保留了這些代碼。
<p> <pre> <a name="x2294"></a><a name="x2290"></a> pix.<a href="qpixmap.html#fill">fill</a>( this, cr.<a href="qrect.html#topLeft">topLeft</a>() );
</pre>
<p> 我們用這個pixmap來充滿這個窗口部件的背景。
<p> <pre> <a href="qpainter.html">QPainter</a> p( &pix );
<a name="x2286"></a> p.<a href="qpainter.html#setBrush">setBrush</a>( blue );
<a name="x2287"></a> p.<a href="qpainter.html#setPen">setPen</a>( NoPen );
<a name="x2291"></a><a name="x2288"></a> p.<a href="qpainter.html#translate">translate</a>( 0, pix.<a href="qpixmap.html#height">height</a>() - 1 );
<a name="x2281"></a> p.<a href="qpainter.html#drawPie">drawPie</a>( QRect( -35,-35, 70, 70 ), 0, 90*16 );
<a name="x2285"></a> p.<a href="qpainter.html#rotate">rotate</a>( -ang );
<a name="x2283"></a> p.<a href="qpainter.html#drawRect">drawRect</a>( QRect(33, -4, 15, 8) );
<a name="x2284"></a> p.<a href="qpainter.html#end">end</a>();
</pre>
<p> 我們就像第九章中一樣畫,但是現在我們是在pixmap上畫。
<p> 在這一點上,我們有一個繪畫工具變量和一個pixmap看起來相當正確,但是我們還沒有在屏幕上畫呢。
<p> <pre> <a name="x2280"></a> p.<a href="qpainter.html#begin">begin</a>( this );
<a name="x2282"></a> p.<a href="qpainter.html#drawPixmap">drawPixmap</a>( cr.<a href="qrect.html#topLeft">topLeft</a>(), pix );
</pre>
<p> 所以我們在CannonField上面打開繪圖工具并在這之后畫這個pixmap。
<p> 這就是全部了。在頂部和底部各有一對線,并且這個代碼是100%不閃爍的。
<p> <pre> QRect CannonField::cannonRect() const
{
<a href="qrect.html">QRect</a> r( 0, 0, 50, 50 );
<a name="x2292"></a> r.<a href="qrect.html#moveBottomLeft">moveBottomLeft</a>( <a href="qwidget.html#rect">rect</a>().bottomLeft() );
return r;
}
</pre>
<p> 這個函數返回一個在窗口部件坐標中封裝加農炮的矩形。首先我們創建一個50*50大小的矩形,然后移動它,使它的左下角和窗口部件自己的左下角相等。
<p> <a href="qwidget.html#rect">QWidget::rect</a>()函數在窗口部件自己的坐標(左上角是0,0)中返回窗口部件封裝的矩形。
<p> <h3> <a href="t10-main-cpp.html">t10/main.cpp</a>
</h3>
<a name="1-3"></a><p>
<p> <pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qwidget.html">QWidget</a>( parent, name )
{
</pre>
<p> 構造函數也是一樣,但是已經加入了一些東西。
<p> <pre> LCDRange *force = new LCDRange( this, "force" );
force->setRange( 10, 50 );
</pre>
<p> 我們加入了第二個LCDRange,用來設置力量。
<p> <pre> <a href="qobject.html#connect">connect</a>( force, SIGNAL(valueChanged(int)),
cannonField, SLOT(setForce(int)) );
<a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(forceChanged(int)),
force, SLOT(setValue(int)) );
</pre>
<p> 我們把<tt>force</tt>窗口部件和<tt>cannonField</tt>窗口部件連接起來,就和我們對<tt>angle</tt>窗口部件做的一樣。
<p> <pre> <a href="qvboxlayout.html">QVBoxLayout</a> *leftBox = new <a href="qvboxlayout.html">QVBoxLayout</a>;
<a name="x2297"></a> grid-><a href="qgridlayout.html#addLayout">addLayout</a>( leftBox, 1, 0 );
leftBox-><a href="qboxlayout.html#addWidget">addWidget</a>( angle );
leftBox-><a href="qboxlayout.html#addWidget">addWidget</a>( force );
</pre>
<p> 在第九章,我們把<tt>angle</tt>放到了布局的左下單元。現在我們想在這個單元中放入兩個窗口部件,所一個我們用了一個垂直的盒子,把這個垂直的盒子放到這個網格單元中,并且把<tt>angle</tt>和<tt>force</tt>放到這個垂直的盒子中。
<p> <pre> force->setValue( 25 );
</pre>
<p> 我們初始化力量的值為25。
<p> <h2> 行為
</h2>
<a name="2"></a><p> 閃爍已經走了,并且我們還有一個力量控制。
<p> (請看<a href="tutorial1-07.html#compiling">編譯</a>來學習如何創建一個makefile和連編應用程序。)
<p> <h2> 練習
</h2>
<a name="3"></a><p> 讓加農炮的炮筒的大小依賴于力量。
<p> 把加農炮放到右下角。
<p> 試著加入一個更好的鍵盤接口。例如,用+和-來增加或者減少力量,用enter來發射。提示:<a href="qaccel.html">QAccel</a>和在LCDRange中新建addStep()和subtractStep(),就像<a href="qslider.html#addStep">QSlider::addStep</a>()。如果你被左面和右面鍵所苦惱(我就是!),試著都改變!
<p> 現在你可以進行<a href="tutorial1-11.html">第十一章</a>了。
<p> [<a href="tutorial1-09.html">上一章</a>]
[<a href="tutorial1-11.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>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -