Namespaces
Variants
Actions

How to use QTimer in Qt

Jump to: navigation, search


Article Metadata

Code Example
Tested with
Devices(s): Symbian emulator

Compatibility
Platform(s): Qt

Article
Keywords: QTimer
Created: james1980 (28 Dec 2008)
Last edited: hamishwillee (11 Oct 2012)

Introduction

The construction of a QTimer can be done with following code:

iTimer = new QTimer(this);

And the timeout value for it can be set in milliseconds with setInterval() function, for example if you want to have one seconds timeout, you could use following code:

iTimer->setInterval(1000);
iTimer->start();

Or simply use the overload of the start() function to set the interval and start the time at the same time:

iTimer->start(1000);

And stopping is handled with stop() function, thus you can stop the timer like this:

iTimer->stop();

When the timeout expires, the QTimer will emit the timeout() signal which you can connect to your own slot:

QObject::connect(iTimer, SIGNAL(timeout()), this, SLOT(TimeOut()));

Note that by default QTimer will continue generates timeout signals until you stop it, to make the QTimer expire only once, you could set this behavior on by calling setSingleShot with true argument.


Example project

File:QtEggAlarm.zip

This page was last modified on 11 October 2012, at 04:17.
1065 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved