📄 aclock-example.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/aclock/aclock.doc:4 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Analog Clock</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; }--></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>Analog Clock</h1> <p> This example displays an analog clock widget.<p> <hr><p> Header file:<p> <pre>/****************************************************************************** $Id: qt/aclock.h 3.0.5 edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef ACLOCK_H#define ACLOCK_H#include <<a href="qwidget-h.html">qwidget.h</a>>#include <<a href="qdatetime-h.html">qdatetime.h</a>>class AnalogClock : public <a href="qwidget.html">QWidget</a> // analog clock widget{ <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public: AnalogClock( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); void setAutoMask(bool b);public slots: void setTime( const <a href="qtime.html">QTime</a> & t );protected: void updateMask(); void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *); void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *); void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *);private slots: void drawClock( <a href="qpainter.html">QPainter</a>* ); void timeout();private: <a href="qpoint.html">QPoint</a> clickPos; <a href="qtime.html">QTime</a> time;};#endif // ACLOCK_H</pre><p> <hr><p> Implementation:<p> <pre>/****************************************************************************** $Id: qt/aclock.cpp 3.0.5 edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "aclock.h"#include <<a href="qtimer-h.html">qtimer.h</a>>#include <<a href="qpainter-h.html">qpainter.h</a>>#include <<a href="qbitmap-h.html">qbitmap.h</a>>//// Constructs an analog clock widget that uses an internal QTimer.//<a name="f381"></a>AnalogClock::AnalogClock( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qwidget.html">QWidget</a>( parent, name ){<a name="x1219"></a> time = QTime::<a href="qtime.html#currentTime">currentTime</a>(); // get current time <a href="qtimer.html">QTimer</a> *internalTimer = new <a href="qtimer.html">QTimer</a>( this ); // create internal timer<a name="x1222"></a> <a href="qobject.html#connect">connect</a>( internalTimer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), SLOT(timeout()) );<a name="x1221"></a> internalTimer-><a href="qtimer.html#start">start</a>( 5000 ); // emit signal every 5 seconds}void AnalogClock::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if(<a href="qwidget.html#isTopLevel">isTopLevel</a>()) clickPos = e-><a href="qmouseevent.html#pos">pos</a>() + QPoint(<a href="qwidget.html#geometry">geometry</a>().topLeft() - frameGeometry().topLeft());}<a name="x1223"></a>void AnalogClock::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ){ if(<a href="qwidget.html#isTopLevel">isTopLevel</a>())<a name="x1199"></a> <a href="qwidget.html#move">move</a>( e-><a href="qmouseevent.html#globalPos">globalPos</a>() - clickPos );}void <a name="f382"></a>AnalogClock::setTime( const <a href="qtime.html">QTime</a> & t ){ time = t; timeout();}//// The QTimer::timeout() signal is received by this slot.//void <a name="f383"></a>AnalogClock::timeout(){ <a href="qtime.html">QTime</a> new_time = QTime::<a href="qtime.html#currentTime">currentTime</a>(); // get the current time time = time.addSecs( 5 );<a name="x1220"></a> if ( new_time.<a href="qtime.html#minute">minute</a>() != time.minute() ) { // minute has changed if (<a href="qwidget.html#autoMask">autoMask</a>()) <a href="qwidget.html#updateMask">updateMask</a>(); else <a href="qwidget.html#update">update</a>(); }}void AnalogClock::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ){ if ( <a href="qwidget.html#autoMask">autoMask</a>() ) return; <a href="qpainter.html">QPainter</a> paint( this );<a name="x1208"></a> paint.<a href="qpainter.html#setBrush">setBrush</a>( <a href="qwidget.html#colorGroup">colorGroup</a>().foreground() ); drawClock( &paint );}// If the clock is transparent, we use updateMask()// instead of paintEvent()void AnalogClock::updateMask() // paint clock mask{ <a href="qbitmap.html">QBitmap</a> bm( <a href="qwidget.html#size">size</a>() ); bm.<a href="qpixmap.html#fill">fill</a>( color0 ); //transparent <a href="qpainter.html">QPainter</a> paint;<a name="x1201"></a> paint.<a href="qpainter.html#begin">begin</a>( &bm, this ); paint.<a href="qpainter.html#setBrush">setBrush</a>( color1 ); // use non-transparent color paint.<a href="qpainter.html#setPen">setPen</a>( color1 ); drawClock( &paint );<a name="x1204"></a> paint.<a href="qpainter.html#end">end</a>(); <a href="qwidget.html#setMask">setMask</a>( bm );}//// The clock is painted using a 1000x1000 square coordinate system, in// the a centered square, as big as possible. The painter's pen and// brush colors are used.//void <a name="f384"></a>AnalogClock::drawClock( <a href="qpainter.html">QPainter</a> *paint ){<a name="x1207"></a> paint-><a href="qpainter.html#save">save</a>();<a name="x1211"></a> paint-><a href="qpainter.html#setWindow">setWindow</a>( -500,-500, 1000,1000 );<a name="x1212"></a> <a href="qrect.html">QRect</a> v = paint-><a href="qpainter.html#viewport">viewport</a>(); int d = QMIN( v.<a href="qrect.html#width">width</a>(), v.<a href="qrect.html#height">height</a>() );<a name="x1216"></a><a name="x1210"></a> paint-><a href="qpainter.html#setViewport">setViewport</a>( v.<a href="qrect.html#left">left</a>() + (v.<a href="qrect.html#width">width</a>()-d)/2, v.<a href="qrect.html#top">top</a>() + (v.<a href="qrect.html#height">height</a>()-d)/2, d, d ); // time = QTime::<a href="qtime.html#currentTime">currentTime</a>(); <a href="qpointarray.html">QPointArray</a> pts; paint-><a href="qpainter.html#save">save</a>();<a name="x1206"></a> paint-><a href="qpainter.html#rotate">rotate</a>( 30*(time.hour()%12-3) + time.minute()/2 );<a name="x1214"></a> pts.setPoints( 4, -20,0, 0,-20, 300,0, 0,20 );<a name="x1202"></a> paint-><a href="qpainter.html#drawConvexPolygon">drawConvexPolygon</a>( pts );<a name="x1205"></a> paint-><a href="qpainter.html#restore">restore</a>(); paint-><a href="qpainter.html#save">save</a>(); paint-><a href="qpainter.html#rotate">rotate</a>( (time.minute()-15)*6 ); pts.setPoints( 4, -10,0, 0,-10, 400,0, 0,10 ); paint-><a href="qpainter.html#drawConvexPolygon">drawConvexPolygon</a>( pts ); paint-><a href="qpainter.html#restore">restore</a>(); for ( int i=0; i<12; i++ ) {<a name="x1203"></a> paint-><a href="qpainter.html#drawLine">drawLine</a>( 440,0, 460,0 ); paint-><a href="qpainter.html#rotate">rotate</a>( 30 ); } paint-><a href="qpainter.html#restore">restore</a>();}<a name="x1226"></a>void AnalogClock::<a href="qwidget.html#setAutoMask">setAutoMask</a>(bool b){ if (b) <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( PaletteForeground ); else <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( PaletteBackground ); QWidget::<a href="qwidget.html#setAutoMask">setAutoMask</a>(b);}</pre><p> <hr><p> Main:<p> <pre>/****************************************************************************** $Id: qt/main.cpp 3.0.5 edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "aclock.h"#include <<a href="qapplication-h.html">qapplication.h</a>>int main( int argc, char **argv ){ <a href="qapplication.html">QApplication</a> a( argc, argv ); AnalogClock *clock = new AnalogClock; if ( argc == 2 && strcmp( argv[1], "-transparent" ) == 0 )<a name="x1230"></a> clock-><a href="qwidget.html#setAutoMask">setAutoMask</a>( TRUE );<a name="x1229"></a> clock-><a href="qwidget.html#resize">resize</a>( 100, 100 ); a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( clock );<a name="x1231"></a> clock-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Analog Clock"); clock-><a href="qwidget.html#show">show</a>(); int result = a.<a href="qapplication.html#exec">exec</a>(); delete clock; return result;}</pre><p>See also <a href="examples.html">Examples</a>.<!-- 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 align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -