<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QTimer Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QTimer Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QTimer class provides repetitive and single-shot timers. <a href="#details">More...</a></p> <p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qtimer.html#QTimer">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None)</li><li><div class="fn" />int <b><a href="qtimer.html#interval">interval</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtimer.html#isActive">isActive</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qtimer.html#isSingleShot">isSingleShot</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qtimer.html#setInterval">setInterval</a></b> (<i>self</i>, int <i>msec</i>)</li><li><div class="fn" /><b><a href="qtimer.html#setSingleShot">setSingleShot</a></b> (<i>self</i>, bool <i>asingleShot</i>)</li><li><div class="fn" /><b><a href="qtimer.html#start">start</a></b> (<i>self</i>, int <i>msec</i>)</li><li><div class="fn" /><b><a href="qtimer.html#start-2">start</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qtimer.html#stop">stop</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qtimer.html#timerEvent">timerEvent</a></b> (<i>self</i>, QTimerEvent)</li><li><div class="fn" />int <b><a href="qtimer.html#timerId">timerId</a></b> (<i>self</i>)</li></ul><h3>Static Methods</h3><ul><li><div class="fn" /><b><a href="qtimer.html#singleShot">singleShot</a></b> (int <i>msec</i>, QObject <i>receiver</i>, SLOT()SLOT() <i>member</i>)</li><li><div class="fn" /><b><a href="qtimer.html#singleShot-2">singleShot</a></b> (int <i>msec</i>, callable <i>receiver</i>)</li></ul><h3>Qt Signals</h3><ul><li><div class="fn" />void <b><a href="qtimer.html#timeout">timeout</a></b> ()</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QTimer class provides repetitive and single-shot timers.</p> <p>The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its <a href="qtimer.html#timeout">timeout</a>() signal to the appropriate slots, and call <a href="qtimer.html#start">start</a>(). From then on it will emit the <a href="qtimer.html#timeout">timeout</a>() signal at constant intervals.</p> <p>Example for a one second (1000 millisecond) timer (from the <a href="widgets-analogclock.html">Analog Clock</a> example):</p> <pre class="cpp"> <span class="type">QTimer</span> <span class="operator">*</span>timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QTimer</span>(<span class="keyword">this</span>); connect(timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(update())); timer<span class="operator">-</span><span class="operator">></span>start(<span class="number">1000</span>); </pre> <p>From then on, the <tt>update()</tt> slot is called every second.</p> <p>You can set a timer to time out only once by calling setSingleShot(true). You can also use the static <a href="qtimer.html#singleShot">QTimer.singleShot</a>() function to call a slot after a specified interval:</p> <pre class="cpp"> <span class="type">QTimer</span><span class="operator">.</span>singleShot(<span class="number">200</span><span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(updateCaption())); </pre> <p>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 <a href="qthread.html#exec">QThread.exec</a>(). Qt uses the timer's <a href="qobject.html#thread">thread affinity</a> to determine which thread will emit the <a href="qtimer.html#timeout">timeout()</a> 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.</p> <p>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:</p> <pre class="cpp"> <span class="type">QTimer</span> <span class="operator">*</span>timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QTimer</span>(<span class="keyword">this</span>); connect(timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(processOneThing())); timer<span class="operator">-</span><span class="operator">></span>start(); </pre> <p><tt>processOneThing()</tt> will from then on 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 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; multithreading is now becoming available on more and more platforms, and we expect that zero-millisecond QTimers will gradually be replaced by <a href="qthread.html">QThread</a>s.</p> <a id="accuracy-and-timer-resolution" name="accuracy-and-timer-resolution" /> <h3>Accuracy and Timer Resolution</h3> <p>Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.</p> <p>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.</p> <p>If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.</p> <a id="alternatives-to-qtimer" name="alternatives-to-qtimer" /> <h3>Alternatives to QTimer</h3> <p>An alternative to using QTimer is to call <a href="qobject.html#startTimer">QObject.startTimer</a>() for your object and reimplement the <a href="qobject.html#timerEvent">QObject.timerEvent</a>() event handler in your class (which must inherit <a href="qobject.html">QObject</a>). The disadvantage is that <a href="qtimer.html#timerEvent">timerEvent</a>() does not support such high-level features as single-shot timers or signals.</p> <p>Another alternative to using QTimer is to use <a href="qbasictimer.html">QBasicTimer</a>. It is typically less cumbersome than using <a href="qobject.html#startTimer">QObject.startTimer</a>() directly. See <a href="timers.html">Timers</a> for an overview of all three approaches.</p> <p>Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations.</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QTimer" />QTimer.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a timer with the given <i>parent</i>.</p> <h3 class="fn"><a name="interval" />int QTimer.interval (<i>self</i>)</h3><h3 class="fn"><a name="isActive" />bool QTimer.isActive (<i>self</i>)</h3><h3 class="fn"><a name="isSingleShot" />bool QTimer.isSingleShot (<i>self</i>)</h3><h3 class="fn"><a name="setInterval" />QTimer.setInterval (<i>self</i>, int <i>msec</i>)</h3><h3 class="fn"><a name="setSingleShot" />QTimer.setSingleShot (<i>self</i>, bool <i>asingleShot</i>)</h3><h3 class="fn"><a name="singleShot" />QTimer.singleShot (int <i>msec</i>, <a href="qobject.html">QObject</a> <i>receiver</i>, SLOT()SLOT() <i>member</i>)</h3><p>This static function calls a slot after a given time interval.</p> <p>It is very convenient to use this function because you do not need to bother with a <a href="qobject.html#timerEvent">timerEvent</a> or create a local <a href="qtimer.html">QTimer</a> object.</p> <p>Example:</p> <pre class="cpp"> <span class="preprocessor">#include <QApplication></span> <span class="preprocessor">#include <QTimer></span> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); <span class="type"><a href="qtimer.html">QTimer</a></span><span class="operator">.</span>singleShot(<span class="number">600000</span><span class="operator">,</span> <span class="operator">&</span>app<span class="operator">,</span> SLOT(quit())); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="keyword">return</span> app<span class="operator">.</span>exec(); } </pre> <p>This sample program automatically terminates after 10 minutes (600,000 milliseconds).</p> <p>The <i>receiver</i> is the receiving object and the <i>member</i> is the slot. The time interval is <i>msec</i> milliseconds.</p> <p><b>Note:</b> This function is <a href="threads-reentrancy.html#reentrant">reentrant</a>.</p> <p><b>See also</b> <a href="qtimer.html#singleShot-prop">setSingleShot</a>() and <a href="qtimer.html#start">start</a>().</p> <h3 class="fn"><a name="singleShot-2" />QTimer.singleShot (int <i>msec</i>, callable <i>receiver</i>)</h3><h3 class="fn"><a name="start" />QTimer.start (<i>self</i>, int <i>msec</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void start(int)</tt>.</p><p>Starts or restarts the timer with a timeout interval of <i>msec</i> milliseconds.</p> <p>If the timer is already running, it will be <a href="qtimer.html#stop">stopped</a> and restarted.</p> <p>If <a href="qtimer.html#singleShot-prop">singleShot</a> is true, the timer will be activated only once.</p> <h3 class="fn"><a name="start-2" />QTimer.start (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void start()</tt>.</p><p>This function overloads <a href="qtimer.html#start">start</a>().</p> <p>Starts or restarts the timer with the timeout specified in <a href="qtimer.html#interval-prop">interval</a>.</p> <p>If the timer is already running, it will be <a href="qtimer.html#stop">stopped</a> and restarted.</p> <p>If <a href="qtimer.html#singleShot-prop">singleShot</a> is true, the timer will be activated only once.</p> <h3 class="fn"><a name="stop" />QTimer.stop (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void stop()</tt>.</p><p>Stops the timer.</p> <p><b>See also</b> <a href="qtimer.html#start">start</a>().</p> <h3 class="fn"><a name="timerEvent" />QTimer.timerEvent (<i>self</i>, <a href="qtimerevent.html">QTimerEvent</a>)</h3><p>Reimplemented from <a href="qobject.html#timerEvent">QObject.timerEvent</a>().</p> <h3 class="fn"><a name="timerId" />int QTimer.timerId (<i>self</i>)</h3><p>Returns the ID of the timer if the timer is running; otherwise returns -1.</p> <hr /><h2>Qt Signal Documentation</h2><h3 class="fn"><a name="timeout" />void timeout ()</h3><p>This is the default overload of this signal.</p><p>This signal is emitted when the timer times out.</p> <p><b>See also</b> <a href="qtimer.html#interval-prop">interval</a>, <a href="qtimer.html#start">start</a>(), and <a href="qtimer.html#stop">stop</a>().</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.10.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt 4.8.5</td></tr></table></div></address></body></html>