Qtimer Signal Slot Example
Home All Classes Main Classes Annotated Grouped Classes Functions |
The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout signal to the appropriate slots, and call start. From then on, it will emit the timeout signal at constant intervals. Example for a one second (1000 millisecond) timer (from the Analog Clock example).
The QTimer class provides timer signals and single-shot timers.More...
#include <qtimer.h>
Inherits QObject.
- A QTimer is used in this project to create a 1s timer tick that calls a function every 1s time period. 3.1 Add a QTimer to the Main Window Class. Open mainwindow.h and add a pointer to a QTimer object as shown below. Private: QTimer.timer1s; Scroll down for a full listing of the mainwindow.h file that includes the above code.
- Cross-thread signal-slot connections are implemented by dispatching a QMetaCallEvent to the target object. A QObject instance can be moved to a thread, where it will process its events, such as timer events or slot/method calls. To do work on a thread, first create your own worker class that derives from QObject. Then move it to the thread.
Public Members
- QTimer ( QObject * parent = 0, const char * name = 0 )
- ~QTimer ()
- bool isActive () const
- int 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 )
Detailed Description
The QTimer class provides timer signals and single-shot timers.It uses timer events internally toprovide a more versatile timer. QTimer is very easy to use:create a QTimer, call start() to start it and connect itstimeout() to the appropriate slots. When the time is up it willemit the timeout() signal.
Note that a QTimer object is destroyed automatically when itsparent object is destroyed.
Example:
You can also use the static singleShot() function to create asingle shot timer.
As a special case, a QTimer with timeout 0 times out as soon asall the events in the window system's event queue have beenprocessed.
This can be used to do heavy work while providing a snappyuser interface:
myObject->processOneThing() will be called repeatedly and shouldreturn quickly (typically after processing one data item) so thatQt can deliver events to widgets and stop the timer as soon as ithas done all its work. This is the traditional way ofimplementing heavy work in GUI applications; multi-threading isnow becoming available on more and more platforms, and we expectthat null events will eventually be replaced by threading.
Note that QTimer's accuracy depends on the underlying operatingsystem and hardware. Most platforms support an accuracy of 20ms;some provide more. If Qt is unable to deliver the requestednumber of timer clicks, it will silently discard some.
An alternative to using QTimer is to call QObject::startTimer()for your object and reimplement the QObject::timerEvent() eventhandler in your class (which must, of course, inherit QObject).The disadvantage is that timerEvent() does not support suchhigh-level features as single-shot timers or signals.
Some operating systems limit the number of timers that may beused; Qt tries to work around these limitations.
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 timerobject.
QTimer::~QTimer ()
Destroys the timer.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.
See also start() and isActive().
bool QTimer::isActive () const
Returns TRUE if the timer is running (pending); otherwise returnsFALSE.
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 needto bother with a timerEvent orto create a local QTimer object.
Example:
This sample program automatically terminates after 10 minutes (i.e.600000 milliseconds).
The receiver is the receiving object and the member is theslot. The time interval is msec.
int QTimer::start ( int msec, bool sshot = FALSE )
Starts the timer with a msec milliseconds timeout, and returnsthe 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.
Any pending timer will be stopped.
See also singleShot(), stop(), changeInterval(), and isActive().
Examples: aclock/aclock.cpp, dirview/dirview.cpp, distributor/distributor.ui.h, forever/forever.cpp, hello/hello.cpp, t11/cannon.cpp, and t13/cannon.cpp.
void QTimer::stop ()
Stops the timer.See also start().
Examples: dirview/dirview.cpp, t11/cannon.cpp, t12/cannon.cpp, and t13/cannon.cpp.
void QTimer::timeout () [signal]
This signal is emitted when the timer is activated.
Examples: aclock/aclock.cpp, dirview/dirview.cpp, distributor/distributor.ui.h, forever/forever.cpp, hello/hello.cpp, and t11/cannon.cpp.
int QTimer::timerId () const
Returns the ID of the timer if the timer is running; otherwise returns-1.
This file is part of the Qt toolkit.Copyright © 1995-2005Trolltech. All Rights Reserved.
Hi,
I cannot figure out how moveToThread is working with QTimer class member.
I have a simple code example of my problem.
Description of the code example:
MyObject1:
Create MyObject2.
Move MyObject2 to a thread using moveToThread.
Connect the started signal from QThread to the MyObject1 slot process.
MyObject2:
Class that inherits from QObject.
Contain a QTimer member.
Contain a process slot.
Running behavior:
In the process slot of MyObject2 I start my QTimer class member and I get:
@QObject::startTimer: timers cannot be started from another thread@
Resolve the problem:
I can get rid of this, by defining my QTimer member as a pointer and create it in the process slot.
Question:
What I do not understand is why in the case that my QTimer class member is not a pointer, my timer is running in a different thread?
moveToThread should make it running in the same thread as MyObject2 thread, no?
Code example:
@#include <QDebug>
#include <QThread>
#include 'mythread1.h'
#include 'mythread2.h'
MyObject1::MyObject1(QObject *parent) :
QObject(parent)
{
qDebug() << 'Constructor object1, THREAD :' << QThread::currentThreadId();
}
#ifndef MYTHREAD1_H
#define MYTHREAD1_H
Qtimer Signal Slot Examples
#include 'mythread2.h'
class MyObject1 : public QObject
{
Q_OBJECT
public:
explicit MyObject1(QObject *parent = 0);
signals:
public slots:
protected:
private:
};
#endif // MYTHREAD1_H
#include <QDebug>
#include <QThread>
#include 'mythread2.h'
MyObject2::MyObject2(QObject *parent) :
QObject(parent)
{
qDebug() << 'Constructor object2, THREAD :' << QThread::currentThreadId();
}
void MyObject2::process()
{
qDebug() << 'Object2' << func << 'THREAD :' << QThread::currentThreadId();
m_timer.setInterval(5000);
connect (&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
m_timer.start();
}
void MyObject2::onTimeout()
{
qDebug() << func << 'Object2 THREAD :' << QThread::currentThreadId();
}
#ifndef MYTHREAD2_H
#define MYTHREAD2_H
Qtimer Start
#include <QTimer>
class MyObject2 : public QObject
{
Q_OBJECT
public:
explicit MyObject2(QObject *parent = 0);
signals:
Qt Qtimer Example
public slots:
void process(void);
void onTimeout(void);
protected:
};
#endif // MYTHREAD2_H@
Qtimer Pyqt
Thanks in advance for your help.
Qtimer Timeout
Julien.