Return to site

Qtimer Signal Slot

broken image


Example

Some times you see a signal is emitted in sender thread but connected slot doesn't called (in other words it doesn't receive signal), you have asked about it and finaly got that the connection type Qt::DirectConnection would fix it, so the problem found and everything is ok.

Hi I'd like to do something like this: QTimer::singleShot(5000, this, SLOT(MySlot(iID))); to get the ID (can be 1 to 16) and know which ID did kick off the singleShot. However, that seems not to be possible with on-board tools. Any idea how this can be e. When a QTimer times out, every Signal-Slot connection, which joins signal of this QTimer with an slot, fires this slot exactly one time.

Qtimer Signal Slot Games

But generaly this is bad idea to use Qt:DirectConnection until you really know what is this and there is no other way. Lets explain it more, Each thread created by Qt (including main thread and new threads created by QThread) have Event loop, the event loop is responsible for receiving signals and call aproporiate slots in its thread. Generaly executing a blocking operation inside an slot is bad practice, because it blocks the event loop of that threads so no other slots would be called.

If you block an event loop (by making very time consuming or blocking operation) you will not receive events on that thread until the event loop will be unblocked. If the blocking operation, blocks the event loop forever (such as busy while), the slots could never be called.

In this situation you may set the connection type in connect to Qt::DirectConnection, now the slots will be called even the event loop is blocked. so how this could make broke everything? In Qt::DirectConnection Slots will be called in emiter threads, and not receiver threads and it can broke data synchronizations and ran into other problems. So never use Qt::DirectConnection unless you know what are you doing. If your problem will be solved by using Qt::DirectConnection, you have to carefull and look at your code and finding out why your event loop is blocked. Its not a good idea to block the event loop and its not recomended in Qt.

Here is small example which shows the problem, as you can see the nonBlockingSlot would be called even the blockingSlot blocked event loop with while(1) which indicates bad coding



Related Tags

The QTimer class provides repetitive and single-shot timers. More..

Header:#include
qmake: QT += core
Inherits:QObject
Inherited By:

Properties

  • active : const bool
  • interval : int
  • remainingTime : const int
  • singleShot : bool
  • timerType : Qt::TimerType
  • 1 property inherited from QObject

Public Functions

QTimer(QObject * parent = 0)
~QTimer()
int interval() const
bool isActive() const
bool isSingleShot() const
int remainingTime() const
void setInterval(int msec)
void setSingleShot(bool singleShot)
void setTimerType(Qt::TimerType atype)
int timerId() const
Qt::TimerType timerType() const
  • 31 public functions inherited from QObject

Public Slots

  • 1 public slot inherited from QObject

Signals

void timeout()
  • 2 signals inherited from QObject

Static Public Members

void singleShot(int msec, const QObject * receiver, const char * member)
void singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, const char * member)
void singleShot(int msec, const QObject * receiver, PointerToMemberFunction method)
void singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, PointerToMemberFunction method)
void singleShot(int msec, Functor functor)
void singleShot(int msec, Qt::TimerType timerType, Functor functor)
void singleShot(int msec, const QObject * context, Functor functor)
void singleShot(int msec, Qt::TimerType timerType, const QObject * context, Functor functor)
  • 11 static public members inherited from QObject

Reimplemented Protected Functions

  • 9 protected functions inherited from QObject

Detailed Description

The QTimer class provides repetitive and single-shot timers.

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):

From then on, the update() slot is called every second.

You can set a timer to time out only once by calling setSingleShot(true). You can also use the static QTimer::singleShot() function to call a slot after a specified interval:

In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec(). Qt uses the timer's thread affinity to determine which thread will emit the timeout() signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.

As a special case, a QTimer with a timeout of 0 will time out as soon as all the events in the window system's event queue have been processed. This can be used to do heavy work while providing a snappy user interface:

From then on, processOneThing() will be called repeatedly. It should be written in such a way that it always returns quickly (typically after processing one data item) so that Qt can deliver events to the user interface 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, but as multithreading is nowadays becoming available on more and more platforms, we expect that zero-millisecond QTimer objects will gradually be replaced by QThreads.

Accuracy and Timer Resolution

The accuracy of timers depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.

The accuracy also depends on the timer type. For Qt::PreciseTimer, QTimer will try to keep the accurance at 1 millisecond. Precise timers will also never time out earlier than expected.

For Qt::CoarseTimer and Qt::VeryCoarseTimer types, QTimer may wake up earlier than expected, within the margins for those types: 5% of the interval for Qt::CoarseTimer and 500 ms for Qt::VeryCoarseTimer.

All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy. In such a case of timeout overrun, Qt will emit activated() only once, even if multiple timeouts have expired, and then will resume the original interval.

Alternatives to QTimer

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 inherit QObject). The disadvantage is that timerEvent() does not support such high-level features as single-shot timers or signals.

Another alternative is QBasicTimer. It is typically less cumbersome than using QObject::startTimer() directly. See Timers for an overview of all three approaches.

Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations.

See also QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, Analog Clock Example, and Wiggly Example.

Property Documentation

active : const bool

Qt Qtimer Signal Slot

This boolean property is true if the timer is running; otherwise false.

Slot

This property was introduced in Qt 4.3.

Access functions:

interval : int

This property holds the timeout interval in milliseconds.

The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.

Setting the interval of an active timer changes its timerId().

Access functions:

int interval() const
void setInterval(int msec)

See also singleShot.

remainingTime : const int

This property holds the remaining time in milliseconds.

Returns the timer's remaining value in milliseconds left until the timeout. If the timer is inactive, the returned value will be -1. If the timer is overdue, the returned value will be 0.

This property was introduced in Qt 5.0.

Access functions:

See also interval.

singleShot : bool

This property holds whether the timer is a single-shot timer.

A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.

Access functions:

bool isSingleShot() const
void setSingleShot(bool singleShot)

See also interval and singleShot().

timerType : Qt::TimerType

This property holds controls the accuracy of the timer.

The default value for this property is Qt::CoarseTimer.

Access functions:

Qt::TimerType timerType() const
void setTimerType(Qt::TimerType atype)

See also Qt::TimerType.

Member Function Documentation

Qtimer Signal Slot

QTimer::QTimer(QObject * parent = 0)

Constructs a timer with the given parent.

QTimer::~QTimer()

Destroys the timer.

bool QTimer::isActive() const

Returns true if the timer is running (pending); otherwise returns false.

Note: Getter function for property active.

[static] void QTimer::singleShot(int msec, const QObject * receiver, const char * member)

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 create a local QTimer object.

Example:

This sample program automatically terminates after 10 minutes (600,000 milliseconds).

Qtimer Signal Slot Car

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.

Note: This function is reentrant.

See also setSingleShot() and start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, const char * member)

This is an overloaded function.

Qtimer Signal Slot Machine

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 create a local QTimer object.

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

SIGN UP FOR PROMOTIONS AND UPCOMING EVENTS. About Us; Gaming; Events; Victory Card; Groups; Transportation. Casino cruise in jacksonville fl.

Note: This function is reentrant.

See also start().

[static] void QTimer::singleShot(int msec, const QObject * receiver, PointerToMemberFunction method)

This is an overloaded function.

This static function calls a member function of a QObject after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The receiver is the receiving object and the method is the member function. The time interval is msec milliseconds.

Qtimer Signal Slot

If receiver is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of receiver. The receiver's thread must have a running Qt event loop.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, PointerToMemberFunction method)

Qtimer Signal Slot Example

This is an overloaded function.

This static function calls a member function of a QObject after a given time interval.

Qtimer Signal Slot

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The receiver is the receiving object and the method is the member function. The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

If receiver is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of receiver. The receiver's thread must have a running Qt event loop.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

Qtimer Signal Slot Game

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, const QObject * context, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds.

If context is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of context. The context's thread must have a running Qt event loop.

Qt qtimer signal slot

This property was introduced in Qt 4.3.

Access functions:

interval : int

This property holds the timeout interval in milliseconds.

The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.

Setting the interval of an active timer changes its timerId().

Access functions:

int interval() const
void setInterval(int msec)

See also singleShot.

remainingTime : const int

This property holds the remaining time in milliseconds.

Returns the timer's remaining value in milliseconds left until the timeout. If the timer is inactive, the returned value will be -1. If the timer is overdue, the returned value will be 0.

This property was introduced in Qt 5.0.

Access functions:

See also interval.

singleShot : bool

This property holds whether the timer is a single-shot timer.

A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.

Access functions:

bool isSingleShot() const
void setSingleShot(bool singleShot)

See also interval and singleShot().

timerType : Qt::TimerType

This property holds controls the accuracy of the timer.

The default value for this property is Qt::CoarseTimer.

Access functions:

Qt::TimerType timerType() const
void setTimerType(Qt::TimerType atype)

See also Qt::TimerType.

Member Function Documentation

QTimer::QTimer(QObject * parent = 0)

Constructs a timer with the given parent.

QTimer::~QTimer()

Destroys the timer.

bool QTimer::isActive() const

Returns true if the timer is running (pending); otherwise returns false.

Note: Getter function for property active.

[static] void QTimer::singleShot(int msec, const QObject * receiver, const char * member)

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 create a local QTimer object.

Example:

This sample program automatically terminates after 10 minutes (600,000 milliseconds).

Qtimer Signal Slot Car

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.

Note: This function is reentrant.

See also setSingleShot() and start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, const char * member)

This is an overloaded function.

Qtimer Signal Slot Machine

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 create a local QTimer object.

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

SIGN UP FOR PROMOTIONS AND UPCOMING EVENTS. About Us; Gaming; Events; Victory Card; Groups; Transportation. Casino cruise in jacksonville fl.

Note: This function is reentrant.

See also start().

[static] void QTimer::singleShot(int msec, const QObject * receiver, PointerToMemberFunction method)

This is an overloaded function.

This static function calls a member function of a QObject after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The receiver is the receiving object and the method is the member function. The time interval is msec milliseconds.

If receiver is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of receiver. The receiver's thread must have a running Qt event loop.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject * receiver, PointerToMemberFunction method)

Qtimer Signal Slot Example

This is an overloaded function.

This static function calls a member function of a QObject after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The receiver is the receiving object and the method is the member function. The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

If receiver is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of receiver. The receiver's thread must have a running Qt event loop.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

Qtimer Signal Slot Game

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, const QObject * context, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds.

If context is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of context. The context's thread must have a running Qt event loop.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[static] void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject * context, Functor functor)

This is an overloaded function.

This static function calls functor after a given time interval.

It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.

The time interval is msec milliseconds. The timerType affects the accuracy of the timer.

If context is destroyed before the interval occurs, the method will not be called. The function will be run in the thread of context. The context's thread must have a running Qt event loop.

Note: This function is reentrant.

This function was introduced in Qt 5.4.

See also start().

[slot] void QTimer::start(int msec)

Starts or restarts the timer with a timeout interval of msec milliseconds.

If the timer is already running, it will be stopped and restarted.

If singleShot is true, the timer will be activated only once.

[slot] void QTimer::start()

This function overloads start().

Starts or restarts the timer with the timeout specified in interval.

If the timer is already running, it will be stopped and restarted.

If singleShot is true, the timer will be activated only once.

[slot] void QTimer::stop()

Stops the timer.

See also start().

[signal] void QTimer::timeout()

This signal is emitted when the timer times out.

Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.

See also interval, start(), and stop().

[virtual protected] void QTimer::timerEvent(QTimerEvent * e)

Reimplemented from QObject::timerEvent().

int QTimer::timerId() const

Returns the ID of the timer if the timer is running; otherwise returns -1.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.





broken image