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.

Handling Events in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): N8
Platform(s): Symbian
Keywords: QLCDNumber, timerEvent()
Created: somnathbanik (05 May 2015)
Last edited: hamishwillee (11 Oct 2012)



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

ClockWidgetQt1.png ClockWidgetQt2.png

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.

#include <QMainWindow>
#include <QLCDNumber>
namespace Ui {
class ClockWidget;
class QTimerEvent;
class ClockWidget : public QLCDNumber
explicit ClockWidget(QWidget *parent = 0);
Ui::ClockWidget *ui;
void timerEvent(QTimerEvent *e);
int updateTimer, switchTimer;
bool showClock;

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.

#include "clockwidget.h"
#include <QtGui>
ClockWidget::ClockWidget(QWidget *parent) :
QLCDNumber(parent), showClock(true)
updateTimer = startTimer(1000);
switchTimer = startTimer(10000);
QTimerEvent *e = new QTimerEvent(updateTimer);
QCoreApplication::postEvent(this, e);

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.

void ClockWidget::timerEvent(QTimerEvent *e)
if (!e) return;
if (e->timerId() == switchTimer)
showClock = !showClock;
if (e->timerId() == updateTimer) {
if (showClock) {
QTime time = QTime::currentTime();
QString str = time.toString(Qt::LocalDate);
} else {
QDate date = QDate::currentDate();
QString str = date.toString(Qt::LocalDate);

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.

Source Code

The full source code presented in this article is available here

Reference Articles

  1. Archived:QTimer example
  2. Archived:Creating a Digital Clock QWidget

--somnathbanik 12:49, 5 May 2011 (UTC)

This page was last modified on 11 October 2012, at 01:17.
40 page views in the last 30 days.