1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/digitalclock.qdoc -->
7
<title>Qt 4.0: Digital Clock Example</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" 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>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Digital Clock Example</h1>
23
<li><a href="widgets-digitalclock-digitalclock-cpp.html">widgets/digitalclock/digitalclock.cpp</a></li>
24
<li><a href="widgets-digitalclock-digitalclock-h.html">widgets/digitalclock/digitalclock.h</a></li>
25
<li><a href="widgets-digitalclock-main-cpp.html">widgets/digitalclock/main.cpp</a></li>
27
<p>The Digital Clock example shows how to use <a href="qlcdnumber.html">QLCDNumber</a> to display a number with LCD-like digits.</p>
28
<center><img src="images/digitalclock-example.png" alt="Screenshot of the Digital Clock example" /></center><p>This example also demonstrates how <a href="qtimer.html">QTimer</a> can be used to update a widget at regular intervals.</p>
29
<a name="digitalclock-class-definition"></a>
30
<h2>DigitalClock Class Definition</h2>
31
<p>The <tt>DigitalClock</tt> class provides a clock widget showing the time with hours and minutes separated by a blinking colon. We subclass <a href="qlcdnumber.html">QLCDNumber</a> and implement a private slot called <tt>showTime()</tt> to update the clock display:</p>
32
<pre> class DigitalClock : public QLCDNumber
37
DigitalClock(QWidget *parent = 0);
42
<a name="digitalclock-class-implementation"></a>
43
<h2>DigitalClock Class Implementation</h2>
44
<pre> DigitalClock::DigitalClock(QWidget *parent)
47
setSegmentStyle(Filled);
49
QTimer *timer = new QTimer(this);
50
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
51
timer->start(1000);
55
setWindowTitle(tr("Digital Clock"));
58
<p>In the constructor, we first change the look of the LCD numbers. The <a href="qlcdnumber.html#SegmentStyle-enum">QLCDNumber::Filled</a> style produces raised segments filled with the foreground color (typically black). We also set up a one-second timer to keep track of the current time, and we connect its <a href="qtimer.html#timeout">timeout()</a> signal to the private <tt>showTime()</tt> slot so that the display is updated every second. Then, we call the <tt>showTime()</tt> slot; without this call, there would be a one-second delay at startup before the time is shown.</p>
59
<pre> void DigitalClock::showTime()
61
QTime time = QTime::currentTime();
62
QString text = time.toString("hh:mm");
63
if ((time.second() % 2) == 0)
67
<p>The <tt>showTime()</tt> slot is called whenever the clock display needs to be updated.</p>
68
<p>The current time is converted into a string with the format "hh:mm". When <a href="qtime.html#second">QTime::second</a>() is a even number, the colon in the string is replaced with a space. This makes the colon appear and vanish every other second.</p>
69
<p>Finally, we call <a href="qlcdnumber.html#intValue-prop">QLCDNumber::display</a>() to update the widget.</p>
70
<p /><address><hr /><div align="center">
71
<table width="100%" cellspacing="0" border="0"><tr class="address">
72
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
73
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
74
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
75
</tr></table></div></address></body>