~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/widgets-digitalclock.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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 -->
 
6
<head>
 
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>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>
 
21
<p>Files:</p>
 
22
<ul>
 
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>
 
26
</ul>
 
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>&nbsp;   class DigitalClock : public QLCDNumber
 
33
    {
 
34
        Q_OBJECT
 
35
 
 
36
    public:
 
37
        DigitalClock(QWidget *parent = 0);
 
38
 
 
39
    private slots:
 
40
        void showTime();
 
41
    };</pre>
 
42
<a name="digitalclock-class-implementation"></a>
 
43
<h2>DigitalClock Class Implementation</h2>
 
44
<pre>&nbsp;   DigitalClock::DigitalClock(QWidget *parent)
 
45
        : QLCDNumber(parent)
 
46
    {
 
47
        setSegmentStyle(Filled);
 
48
 
 
49
        QTimer *timer = new QTimer(this);
 
50
        connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
 
51
        timer-&gt;start(1000);
 
52
 
 
53
        showTime();
 
54
 
 
55
        setWindowTitle(tr(&quot;Digital Clock&quot;));
 
56
        resize(150, 60);
 
57
    }</pre>
 
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>&nbsp;   void DigitalClock::showTime()
 
60
    {
 
61
        QTime time = QTime::currentTime();
 
62
        QString text = time.toString(&quot;hh:mm&quot;);
 
63
        if ((time.second() % 2) == 0)
 
64
            text[2] = ' ';
 
65
        display(text);
 
66
    }</pre>
 
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 &quot;hh:mm&quot;. 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 &copy; 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>
 
76
</html>