| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | 
|---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/dclock/dclock.doc:4 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>Digital Clock</title> | 
|---|
| 7 | <style type="text/css"><!-- | 
|---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; } | 
|---|
| 9 | a:link { color: #004faf; text-decoration: none } | 
|---|
| 10 | a:visited { color: #672967; text-decoration: none } | 
|---|
| 11 | body { background: #ffffff; color: black; } | 
|---|
| 12 | --></style> | 
|---|
| 13 | </head> | 
|---|
| 14 | <body> | 
|---|
| 15 |  | 
|---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%"> | 
|---|
| 17 | <tr bgcolor="#E5E5E5"> | 
|---|
| 18 | <td valign=center> | 
|---|
| 19 | <a href="index.html"> | 
|---|
| 20 | <font color="#004faf">Home</font></a> | 
|---|
| 21 | | <a href="classes.html"> | 
|---|
| 22 | <font color="#004faf">All Classes</font></a> | 
|---|
| 23 | | <a href="mainclasses.html"> | 
|---|
| 24 | <font color="#004faf">Main Classes</font></a> | 
|---|
| 25 | | <a href="annotated.html"> | 
|---|
| 26 | <font color="#004faf">Annotated</font></a> | 
|---|
| 27 | | <a href="groups.html"> | 
|---|
| 28 | <font color="#004faf">Grouped Classes</font></a> | 
|---|
| 29 | | <a href="functions.html"> | 
|---|
| 30 | <font color="#004faf">Functions</font></a> | 
|---|
| 31 | </td> | 
|---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Digital Clock</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | <p> | 
|---|
| 36 | This example displays a digital LCD clock that can switch between time | 
|---|
| 37 | and date. | 
|---|
| 38 | <p> <hr> | 
|---|
| 39 | <p> Header file: | 
|---|
| 40 | <p> <pre>/**************************************************************************** | 
|---|
| 41 | ** $Id: dclock-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 42 | ** | 
|---|
| 43 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 44 | ** | 
|---|
| 45 | ** This file is part of an example program for Qt.  This example | 
|---|
| 46 | ** program may be used, distributed and modified without limitation. | 
|---|
| 47 | ** | 
|---|
| 48 | *****************************************************************************/ | 
|---|
| 49 |  | 
|---|
| 50 | #ifndef DCLOCK_H | 
|---|
| 51 | #define DCLOCK_H | 
|---|
| 52 |  | 
|---|
| 53 | #include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>> | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | class DigitalClock : public <a href="qlcdnumber.html">QLCDNumber</a>          // digital clock widget | 
|---|
| 57 | { | 
|---|
| 58 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> | 
|---|
| 59 | public: | 
|---|
| 60 | DigitalClock( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); | 
|---|
| 61 |  | 
|---|
| 62 | protected:                                      // event handlers | 
|---|
| 63 | void        timerEvent( <a href="qtimerevent.html">QTimerEvent</a> * ); | 
|---|
| 64 | void        mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * ); | 
|---|
| 65 |  | 
|---|
| 66 | private slots:                                  // internal slots | 
|---|
| 67 | void        stopDate(); | 
|---|
| 68 | void        showTime(); | 
|---|
| 69 |  | 
|---|
| 70 | private:                                        // internal data | 
|---|
| 71 | void        showDate(); | 
|---|
| 72 |  | 
|---|
| 73 | bool        showingColon; | 
|---|
| 74 | int         normalTimer; | 
|---|
| 75 | int         showDateTimer; | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | #endif // DCLOCK_H | 
|---|
| 80 | </pre> | 
|---|
| 81 |  | 
|---|
| 82 | <p> <hr> | 
|---|
| 83 | <p> Implementation: | 
|---|
| 84 | <p> <pre>/**************************************************************************** | 
|---|
| 85 | ** $Id: dclock-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 86 | ** | 
|---|
| 87 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 88 | ** | 
|---|
| 89 | ** This file is part of an example program for Qt.  This example | 
|---|
| 90 | ** program may be used, distributed and modified without limitation. | 
|---|
| 91 | ** | 
|---|
| 92 | *****************************************************************************/ | 
|---|
| 93 |  | 
|---|
| 94 | #include "dclock.h" | 
|---|
| 95 | #include <<a href="qdatetime-h.html">qdatetime.h</a>> | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | // | 
|---|
| 99 | // Constructs a DigitalClock widget with a parent and a name. | 
|---|
| 100 | // | 
|---|
| 101 |  | 
|---|
| 102 | <a name="f371"></a>DigitalClock::DigitalClock( <a href="qwidget.html">QWidget</a> *parent, const char *name ) | 
|---|
| 103 | : <a href="qlcdnumber.html">QLCDNumber</a>( parent, name ) | 
|---|
| 104 | { | 
|---|
| 105 | showingColon = FALSE; | 
|---|
| 106 | <a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Raised ); | 
|---|
| 107 | <a href="qframe.html#setLineWidth">setLineWidth</a>( 2 );                          // set frame line width | 
|---|
| 108 | showTime();                                 // display the current time | 
|---|
| 109 | normalTimer = <a href="qobject.html#startTimer">startTimer</a>( 500 );            // 1/2 second timer events | 
|---|
| 110 | showDateTimer = -1;                         // not showing date | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 | // | 
|---|
| 115 | // Handles timer events for the digital clock widget. | 
|---|
| 116 | // There are two different timers; one timer for updating the clock | 
|---|
| 117 | // and another one for switching back from date mode to time mode. | 
|---|
| 118 | // | 
|---|
| 119 |  | 
|---|
| 120 | <a name="x1102"></a>void DigitalClock::<a href="qobject.html#timerEvent">timerEvent</a>( <a href="qtimerevent.html">QTimerEvent</a> *e ) | 
|---|
| 121 | { | 
|---|
| 122 | <a name="x1105"></a>    if ( e-><a href="qtimerevent.html#timerId">timerId</a>() == showDateTimer )        // stop showing date | 
|---|
| 123 | stopDate(); | 
|---|
| 124 | else {                                      // normal timer | 
|---|
| 125 | if ( showDateTimer == -1 )              // not showing date | 
|---|
| 126 | showTime(); | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | // | 
|---|
| 131 | // Enters date mode when the left mouse button is pressed. | 
|---|
| 132 | // | 
|---|
| 133 |  | 
|---|
| 134 | void DigitalClock::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ) | 
|---|
| 135 | { | 
|---|
| 136 | <a name="x1101"></a>    if ( e-><a href="qmouseevent.html#button">button</a>() == QMouseEvent::LeftButton )               // left button pressed | 
|---|
| 137 | showDate(); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | // | 
|---|
| 142 | // Shows the current date in the internal lcd widget. | 
|---|
| 143 | // Fires a timer to stop showing the date. | 
|---|
| 144 | // | 
|---|
| 145 |  | 
|---|
| 146 | void <a name="f372"></a>DigitalClock::showDate() | 
|---|
| 147 | { | 
|---|
| 148 | if ( showDateTimer != -1 )                  // already showing date | 
|---|
| 149 | return; | 
|---|
| 150 | <a name="x1098"></a>    <a href="qdate.html">QDate</a> date = QDate::<a href="qdate.html#currentDate">currentDate</a>(); | 
|---|
| 151 | <a href="qstring.html">QString</a> s; | 
|---|
| 152 | <a name="x1103"></a><a name="x1100"></a><a name="x1099"></a>    s.<a href="qstring.html#sprintf">sprintf</a>( "%2d %2d", date.<a href="qdate.html#month">month</a>(), date.<a href="qdate.html#day">day</a>() ); | 
|---|
| 153 | <a href="qlcdnumber.html#display">display</a>( s );                               // sets the LCD number/text | 
|---|
| 154 | showDateTimer = <a href="qobject.html#startTimer">startTimer</a>( 2000 );         // keep this state for 2 secs | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | // | 
|---|
| 158 | // Stops showing the date. | 
|---|
| 159 | // | 
|---|
| 160 |  | 
|---|
| 161 | void <a name="f373"></a>DigitalClock::stopDate() | 
|---|
| 162 | { | 
|---|
| 163 | <a href="qobject.html#killTimer">killTimer</a>( showDateTimer ); | 
|---|
| 164 | showDateTimer = -1; | 
|---|
| 165 | showTime(); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | // | 
|---|
| 169 | // Shows the current time in the internal lcd widget. | 
|---|
| 170 | // | 
|---|
| 171 |  | 
|---|
| 172 | void <a name="f374"></a>DigitalClock::showTime() | 
|---|
| 173 | { | 
|---|
| 174 | showingColon = !showingColon;               // toggle/blink colon | 
|---|
| 175 | <a name="x1104"></a>    <a href="qstring.html">QString</a> s = QTime::<a href="qtime.html#currentTime">currentTime</a>().toString().left(5); | 
|---|
| 176 | if ( !showingColon ) | 
|---|
| 177 | s[2] = ' '; | 
|---|
| 178 | if ( s[0] == '0' ) | 
|---|
| 179 | s[0] = ' '; | 
|---|
| 180 | <a href="qlcdnumber.html#display">display</a>( s );                               // set LCD number/text | 
|---|
| 181 | } | 
|---|
| 182 | </pre> | 
|---|
| 183 |  | 
|---|
| 184 | <p> <hr> | 
|---|
| 185 | <p> Main: | 
|---|
| 186 | <p> <pre>/**************************************************************************** | 
|---|
| 187 | ** $Id: dclock-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 188 | ** | 
|---|
| 189 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 190 | ** | 
|---|
| 191 | ** This file is part of an example program for Qt.  This example | 
|---|
| 192 | ** program may be used, distributed and modified without limitation. | 
|---|
| 193 | ** | 
|---|
| 194 | *****************************************************************************/ | 
|---|
| 195 |  | 
|---|
| 196 | #include "dclock.h" | 
|---|
| 197 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 198 |  | 
|---|
| 199 |  | 
|---|
| 200 | int main( int argc, char **argv ) | 
|---|
| 201 | { | 
|---|
| 202 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 203 | DigitalClock *clock = new DigitalClock; | 
|---|
| 204 | clock-><a href="qwidget.html#resize">resize</a>( 170, 80 ); | 
|---|
| 205 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( clock ); | 
|---|
| 206 | clock-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Digital Clock"); | 
|---|
| 207 | clock-><a href="qwidget.html#show">show</a>(); | 
|---|
| 208 | return a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 209 | } | 
|---|
| 210 | </pre> | 
|---|
| 211 |  | 
|---|
| 212 | <p>See also <a href="examples.html">Examples</a>. | 
|---|
| 213 |  | 
|---|
| 214 | <!-- eof --> | 
|---|
| 215 | <p><address><hr><div align=center> | 
|---|
| 216 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 217 | <td>Copyright © 2007 | 
|---|
| 218 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 219 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 220 | </table></div></address></body> | 
|---|
| 221 | </html> | 
|---|