Tools

Namespaces

Variants

Views

Actions

Search

Contents

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Contents

Overview

In this article we will draw a picture how to handle events by using the specialized event handler timerEvent()

Basic Idea

We will implement a widget that displays the clock time in the local display format and also the current date in the appropriate format, alternating every ten seconds. The display itself should update every second.

The below two screen shots recorded at an interval of ten seconds

Class Definition

We implement the clock in a class called ClockWidget, which we derive from QLCDNumber, a Qt class that provides an imitation of an LCD display.

Here we are particularly interested in, besides the constructor, the specialized event handler timerEvent(), which will update the clock time. In the updateTimer and switchTimer member variables we save numbers that serve as identifiers for the timers. The showClock status flag determines whether the clock time (showClock=true) or the date (showClock=false) appears on the widget.

Class Implementation

The implementation in clockwidget.cpp begins by specifying the form of the display. Usually QLCDNumber shows a frame around the digital display. This behavior, inherited from QFrame, is disabled by the QFrame::NoFrame frame style. In addition we dissuade the widget from drawing the LCD elements with shadows and a border, by passing on QLCDNumber::Flat to the widget’s setSegmentStyle() method.

Now we need two timers. Each QObject can start a timer using the startTimer() method. As an argument startTimer() expects the number of seconds that must pass before it triggers a QTimerEvent, which is addressed to the current widget. Each QTimerEvent in turn contains an identification number, which is returned by the invocation of startTimer() that originates it. We manually send a timer event with the ID of the update-Timer, using the postEvent() method of QCoreApplication, so that we do not have to wait for a second to elapse before the time appears on the widget’s display.

In the timerEvent() method we first check whether the pointer to the event really is valid—just to be on the safe side. Next, if the event contains the switchTimer ID, this only toggles the showClock variable. The actual work awaits in the last conditional statement, which is triggered by an event containing the updateTimer ID.

If the widget is supposed to display the time, then we first determine the current time. In Qt, the QTime class is responsible for handling time. The currentTime() static method of this provides the current system time in a QTime object. This time is converted by toString() into a QString. Qt::LocalDate instructs the method to take into account the country settings (locales)of the user. Finally we must inform the display how many LCD digit positions are required. We deduce this from the string length and display the string with display().

QLCDNumber cannot display all alphanumerical characters, we use setNumDigits() for that. On the other hand, if showClock is set to false, which means that the widget should display just the date, we proceed in the same way with the QDate class, which in Qt is responsible for managing date specifications, and whose API corresponds almost exactly to that of QTime.