<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- digitalclock.qdoc --> <title>Digital Clock Example | Qt Widgets 5.9</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-widgets.html">Qt Widgets Examples</a></td><td >Digital Clock Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#digitalclock-class-definition">DigitalClock Class Definition</a></li> <li class="level1"><a href="#digitalclock-class-implementation">DigitalClock Class Implementation</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Digital Clock Example</h1> <span class="subtitle"></span> <!-- $$$widgets/digitalclock-description --> <div class="descr"> <a name="details"></a> <div class="border"><p class="centerAlign"><img src="images/digitalclock-example.png" alt="" /></p></div><p class="figCaption">Screenshot of the Digital Clock example</p> <p>This example also demonstrates how <a href="../qtcore/qtimer.html">QTimer</a> can be used to update a widget at regular intervals.</p> <a name="digitalclock-class-definition"></a> <h2 id="digitalclock-class-definition">DigitalClock Class Definition</h2> <p>The <code>DigitalClock</code> 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 <code>showTime()</code> to update the clock display:</p> <pre class="cpp"> <span class="keyword">class</span> DigitalClock : <span class="keyword">public</span> <span class="type"><a href="qlcdnumber.html">QLCDNumber</a></span> { Q_OBJECT <span class="keyword">public</span>: DigitalClock(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="keyword">private</span> <span class="keyword">slots</span>: <span class="type">void</span> showTime(); }; </pre> <a name="digitalclock-class-implementation"></a> <h2 id="digitalclock-class-implementation">DigitalClock Class Implementation</h2> <pre class="cpp"> DigitalClock<span class="operator">::</span>DigitalClock(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qlcdnumber.html">QLCDNumber</a></span>(parent) { setSegmentStyle(Filled); <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span> <span class="operator">*</span>timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span>(<span class="keyword">this</span>); connect(timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(showTime())); timer<span class="operator">-</span><span class="operator">></span>start(<span class="number">1000</span>); showTime(); setWindowTitle(tr(<span class="string">"Digital Clock"</span>)); resize(<span class="number">150</span><span class="operator">,</span> <span class="number">60</span>); } </pre> <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="../qtcore/qtimer.html#timeout">timeout()</a> signal to the private <code>showTime()</code> slot so that the display is updated every second. Then, we call the <code>showTime()</code> slot; without this call, there would be a one-second delay at startup before the time is shown.</p> <pre class="cpp"> <span class="type">void</span> DigitalClock<span class="operator">::</span>showTime() { <span class="type"><a href="../qtcore/qtime.html">QTime</a></span> time <span class="operator">=</span> <span class="type"><a href="../qtcore/qtime.html">QTime</a></span><span class="operator">::</span>currentTime(); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> time<span class="operator">.</span>toString(<span class="string">"hh:mm"</span>); <span class="keyword">if</span> ((time<span class="operator">.</span>second() <span class="operator">%</span> <span class="number">2</span>) <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) text<span class="operator">[</span><span class="number">2</span><span class="operator">]</span> <span class="operator">=</span> <span class="char">' '</span>; display(text); } </pre> <p>The <code>showTime()</code> slot is called whenever the clock display needs to be updated.</p> <p>The current time is converted into a string with the format "hh:mm". When <a href="../qtcore/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> <p>Finally, we call <a href="qlcdnumber.html#display">QLCDNumber::display</a>() to update the widget.</p> <p>Files:</p> <ul> <li><a href="qtwidgets-widgets-digitalclock-digitalclock-cpp.html">widgets/digitalclock/digitalclock.cpp</a></li> <li><a href="qtwidgets-widgets-digitalclock-digitalclock-h.html">widgets/digitalclock/digitalclock.h</a></li> <li><a href="qtwidgets-widgets-digitalclock-main-cpp.html">widgets/digitalclock/main.cpp</a></li> <li><a href="qtwidgets-widgets-digitalclock-digitalclock-pro.html">widgets/digitalclock/digitalclock.pro</a></li> </ul> </div> <!-- @@@widgets/digitalclock --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>