QTimer(3qt)
π QTimer π
okay bestie, this is the manual page for QTimer from the Qt library β that's the 3qt section of the manual, if you're keeping score πβ¨. It's a whole class for timer signals and single-shot timers, and honestly? It ate. Let's get into it π«Ά
πΈ NAME πΈ
QTimer β timer signals and single-shot timers β°
β¨ SYNOPSIS β¨
#include <qtimer.h>
Inherits QObject. π
π Public Members π
QTimer ( QObject * parent = 0, const char * name = 0 )~QTimer ()bool isActive () constint start ( int msec, bool sshot = FALSE )void changeInterval ( int msec )void stop ()int timerId () const
π¦ Signals π¦
void timeout ()
β Static Public Members β
void singleShot ( int msec, QObject * receiver, const char * member )
π DESCRIPTION π
The QTimer class provides timer signals and single-shot timers. Periodt. β²οΈ
Under the hood it uses timer events to give you a way more versatile timer, and likeβ¦ it's so easy to use. The way that you literally just create a QTimer, call start() to start it, and connect its timeout() to whatever slots you want β and when the time is up, it emits timeout(). Main character energy, no cap. π
Heads up bestie: a QTimer object gets destroyed automatically when its parent object is destroyed. That's just how it is π
Example, because we love a demonstration:
QTimer *timer = new QTimer( myObject );
connect( timer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
timer->start( 2000, TRUE ); // 2 seconds single-shot timer
You can also use the static singleShot() function to create a single shot timer. Easy. π
As a special case β and this is the fun bit β a QTimer with timeout 0 times out as soon as all the events in the window system's event queue have been processed. Iconic behaviour honestly. β¨
This is the move when you wanna do heavy work while still giving the user a snappy interface:
QTimer *t = new QTimer( myObject );
connect( t, SIGNAL(timeout()), SLOT(processOneThing()) );
t->start( 0, FALSE );
myObject->processOneThing() will be called repeatedly and should return quickly (typically after processing one data item) so that Qt can deliver events to widgets and stop the timer as soon as it has done all its work. This is the traditional way of implementing heavy work in GUI applications; multi-threading is now becoming available on more and more platforms, and we expect that null events will eventually be replaced by threading. So likeβ¦ the old way is still the way for now, but change is coming π
Lowkey important:QTimer's accuracy depends on the underlying operating system and hardware. Most platforms support an accuracy of20ms; some provide more. If Qt is unable to deliver the requested number of timer clicks, it will silently discard some. Not a bug, just the vibe. π
An alternative to using QTimer is to call QObject::startTimer() for your object and reimplement the QObject::timerEvent() event handler in your class (which must, of course, inherit QObject). The disadvantage is that timerEvent() does not support such high-level features as single-shot timers or signals. So it's givingβ¦ downgrade, ngl. π
Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations. Bestie Qt is doing its best, okay π«Ά
See also Event Classes and Time and Date. π
π§ MEMBER FUNCTION DOCUMENTATION π§
π« QTimer::QTimer ( QObject * parent = 0, const char * name = 0 )
Constructs a timer called name, with the parent parent.
Note that the parent object's destructor will destroy this timer object. π (That's the whole point of a parent, bestie.)
π« QTimer::~QTimer ()
Destroys the timer. bye girl π
π« void QTimer::changeInterval ( int msec )
Changes the timeout interval to msec milliseconds.
If the timer signal is pending, it will be stopped and restarted; otherwise it will be started. Either way you're getting a fresh interval, slay β¨
See also start() and isActive().
π« bool QTimer::isActive () const
Returns TRUE if the timer is running (pending); otherwise returns FALSE.
Example: t11/cannon.cpp. π―
π« void QTimer::singleShot ( int msec, QObject * receiver, const char * member )
[static]
This static function calls a slot after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or to create a local QTimer object. Literally the lazy-girl path and we respect it π
Example:
#include <qapplication.h>
#include <qtimer.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QTimer::singleShot( 10*60*1000, &a, SLOT(quit()) );
... // create and show your widgets
return a.exec();
}
This sample program automatically terminates after 10 minutes (i.e. 600000 milliseconds). Set it and forget it, periodt β°
The receiver is the receiving object and the member is the slot. The time interval is msec.
π« int QTimer::start ( int msec, bool sshot = FALSE )
Starts the timer with a msec milliseconds timeout, and returns the ID of the timer, or zero when starting the timer failed.
If sshot is TRUE, the timer will be activated only once; otherwise it will continue until it is stopped. One and done, or ride-or-die, your call π¦
Any pending timer will be stopped. π₯
See also singleShot(), stop(), changeInterval(), and isActive().
Examples:
π« void QTimer::stop ()
Stops the timer. π
See also start().
Examples:
π« void QTimer::timeout () [signal]
This signal is emitted when the timer is activated. It's the moment we've all been waiting for β¨
Examples:
π« int QTimer::timerId () const
Returns the ID of the timer if the timer is running; otherwise returns -1.
π SEE ALSO π
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
π AUTHOR π
Generated automatically from the source code. (No human author to thank, bestie, the robot did it π€β¨)
π BUGS π
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. π«Ά
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. (Not me reading the unofficial format π
)
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qtimer.3qt) and the Qt version (3.3.8). Fr fr, include the version, it helps so much π
and that's the whole page, bestie π that was QTimer, manual page QTimer(3qt), brought to you by Trolltech AS, dated 2 February 2007. slay, go start a timer, you've got this β°β¨
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms