📄 aclock-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - aclock/main.cpp example file</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: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Analog Clock</h1><br clear="all"> This example displays an analog clock widget. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/aclock/aclock.h 2.3.8 edited 2004-05-12 $**** 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 QWidget // analog clock widget{ Q_OBJECTpublic: AnalogClock( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); void setAutoMask(bool b);protected: void updateMask(); void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *);private slots: void drawClock( <a href="qpainter.html">QPainter</a>* ); void timeout();private: <a href="qtime.html">QTime</a> time;};#endif // ACLOCK_H</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/aclock/aclock.cpp 2.3.8 edited 2004-05-12 $**** 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.//AnalogClock::AnalogClock( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qwidget.html">QWidget</a>( parent, name ){ time = QTime::currentTime(); // get current time <a href="qtimer.html">QTimer</a> *internalTimer = new <a href="qtimer.html">QTimer</a>( this ); // create internal timer <a href="qobject.html#fbde73">connect</a>( internalTimer, SIGNAL(<a href=#16>timeout</a>()), SLOT(<a href=#16>timeout</a>()) ); internalTimer-><a href="qtimer.html#ce33bc">start</a>( 5000 ); // emit signal every 5 seconds}//// The QTimer::timeout() signal is received by this slot.//void <a name="16"></a>AnalogClock::timeout(){ <a href="qtime.html">QTime</a> new_time = QTime::currentTime(); // get the current time if ( new_time.<a href="qtime.html#83f0e2">minute</a>() != time.minute() ) { // minute has changed if (autoMask()) <a href=#18>updateMask</a>(); else <a href="qwidget.html#a66a88">update</a>(); }}void <a name="17"></a>AnalogClock::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * ){ if ( autoMask() ) return; <a href="qpainter.html">QPainter</a> paint( this ); paint.<a href="qpainter.html#3e0cc8">setBrush</a>( <a href="qwidget.html#d92bf4">colorGroup</a>().foreground() ); <a href=#19>drawClock</a>( &paint );}// If the clock is transparent, we use updateMask()// instead of paintEvent()void <a name="18"></a>AnalogClock::updateMask() // paint clock mask{ <a href="qbitmap.html">QBitmap</a> bm( <a href="qwidget.html#50b75e">size</a>() ); bm.<a href="qpixmap.html#6910a0">fill</a>( color0 ); //transparent <a href="qpainter.html">QPainter</a> paint; paint.<a href="qpainter.html#02ed5d">begin</a>( &bm, this ); paint.<a href="qpainter.html#3e0cc8">setBrush</a>( color1 ); // use non-transparent color paint.<a href="qpainter.html#0183e4">setPen</a>( color1 ); <a href=#19>drawClock</a>( &paint ); paint.<a href="qpainter.html#365784">end</a>(); <a href="qwidget.html#3aa0a1">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="19"></a>AnalogClock::drawClock( <a href="qpainter.html">QPainter</a> *paint ){ paint-><a href="qpainter.html#938d23">save</a>(); paint-><a href="qpainter.html#ad8b2b">setWindow</a>( -500,-500, 1000,1000 ); <a href="qrect.html">QRect</a> v = paint-><a href="qpainter.html#3ea58a">viewport</a>(); int d = QMIN( v.<a href="qrect.html#45fe95">width</a>(), v.<a href="qrect.html#581ab8">height</a>() ); paint-><a href="qpainter.html#42d67a">setViewport</a>( v.<a href="qrect.html#369cab">left</a>() + (v.<a href="qrect.html#45fe95">width</a>()-d)/2, v.<a href="qrect.html#4dd27e">top</a>() + (v.<a href="qrect.html#581ab8">height</a>()-d)/2, d, d ); time = QTime::currentTime(); <a href="qpointarray.html">QPointArray</a> pts; paint-><a href="qpainter.html#938d23">save</a>(); paint-><a href="qpainter.html#b5205c">rotate</a>( 30*(time.hour()%12-3) + time.minute()/2 ); pts.<a href="qpointarray.html#76de1d">setPoints</a>( 4, -20,0, 0,-20, 300,0, 0,20 ); paint-><a href="qpainter.html#2efe17">drawPolygon</a>( pts ); paint-><a href="qpainter.html#ddb3f4">restore</a>(); paint-><a href="qpainter.html#938d23">save</a>(); paint-><a href="qpainter.html#b5205c">rotate</a>( (time.minute()-15)*6 ); pts.<a href="qpointarray.html#76de1d">setPoints</a>( 4, -10,0, 0,-10, 400,0, 0,10 ); paint-><a href="qpainter.html#2efe17">drawPolygon</a>( pts ); paint-><a href="qpainter.html#ddb3f4">restore</a>(); for ( int i=0; i<12; i++ ) { paint-><a href="qpainter.html#e3a489">drawLine</a>( 440,0, 460,0 ); paint-><a href="qpainter.html#b5205c">rotate</a>( 30 ); } paint-><a href="qpainter.html#ddb3f4">restore</a>();}void <a name="20"></a>AnalogClock::setAutoMask(bool b){ if (b) <a href="qwidget.html#e84bbe">setBackgroundMode</a>( PaletteForeground ); else <a href="qwidget.html#e84bbe">setBackgroundMode</a>( PaletteBackground ); <a href="qwidget.html#efa8f9">QWidget::setAutoMask</a>(b);}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/aclock/main.cpp 2.3.8 edited 2004-05-12 $**** 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 name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); AnalogClock *clock = new AnalogClock; if ( argc == 2 && strcmp( argv[1], "-transparent" ) == 0 ) clock-><a name="setAutoMask"></a><a href="qwidget.html#efa8f9">setAutoMask</a>( TRUE ); clock-><a name="resize"></a><a href="qwidget.html#8fcbbe">resize</a>( 100, 100 ); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( clock ); clock-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Analog Clock"); clock-><a name="show"></a><a href="qwidget.html#200ee5">show</a>(); int result = a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>(); delete clock; return result;}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -