Qt 4.8
qtimer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qtimer.h"
44 #include "qcoreapplication.h"
45 #include "qobject_p.h"
46 
48 
141 static const int INV_TIMER = -1; // invalid timer id
142 
148  : QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0)
149 {
150 }
151 
152 
153 #ifdef QT3_SUPPORT
154 
158 QTimer::QTimer(QObject *parent, const char *name)
159  : QObject(parent), id(INV_TIMER), single(0), nulltimer(0)
160 {
162 }
163 #endif
164 
170 {
171  if (id != INV_TIMER) // stop running timer
172  stop();
173 }
174 
175 
232 {
233  if (id != INV_TIMER) // stop running timer
234  stop();
235  nulltimer = (!inter && single);
237 }
238 
249 void QTimer::start(int msec)
250 {
251  inter = msec;
252  start();
253 }
254 
255 
256 #ifdef QT3_SUPPORT
257 
265 int QTimer::start(int msec, bool sshot)
266 {
267  if (id >=0 && nulltimer && !msec && sshot)
268  return id;
269  stop();
270  setInterval(msec);
271  setSingleShot(sshot);
272  start();
273  return timerId();
274 }
275 #endif
276 
277 
285 {
286  if (id != INV_TIMER) {
287  QObject::killTimer(id);
288  id = INV_TIMER;
289  }
290 }
291 
292 
297 {
298  if (e->timerId() == id) {
299  if (single)
300  stop();
301  emit timeout();
302  }
303 }
304 
305 class QSingleShotTimer : public QObject
306 {
307  Q_OBJECT
308  int timerId;
309 public:
310  ~QSingleShotTimer();
311  QSingleShotTimer(int msec, QObject *r, const char * m);
312 Q_SIGNALS:
313  void timeout();
314 protected:
315  void timerEvent(QTimerEvent *);
316 };
317 
318 QSingleShotTimer::QSingleShotTimer(int msec, QObject *receiver, const char *member)
319  : QObject(QAbstractEventDispatcher::instance())
320 {
321  connect(this, SIGNAL(timeout()), receiver, member);
322  timerId = startTimer(msec);
323 }
324 
326 {
327  if (timerId > 0)
329 }
330 
332 {
333  // need to kill the timer _before_ we emit timeout() in case the
334  // slot connected to timeout calls processEvents()
335  if (timerId > 0)
337  timerId = -1;
338  emit timeout();
339 
340  // we would like to use delete later here, but it feels like a
341  // waste to post a new event to handle this event, so we just unset the flag
342  // and explicitly delete...
343  qDeleteInEventHandler(this);
344 }
345 
347 #include "qtimer.moc"
349 
373 void QTimer::singleShot(int msec, QObject *receiver, const char *member)
374 {
375  if (receiver && member) {
376  if (msec == 0) {
377  // special code shortpath for 0-timers
378  const char* bracketPosition = strchr(member, '(');
379  if (!bracketPosition || !(member[0] >= '0' && member[0] <= '3')) {
380  qWarning("QTimer::singleShot: Invalid slot specification");
381  return;
382  }
383  QByteArray methodName(member+1, bracketPosition - 1 - member); // extract method name
385  return;
386  }
387  (void) new QSingleShotTimer(msec, receiver, member);
388  }
389 }
390 
419 void QTimer::setInterval(int msec)
420 {
421  inter = msec;
422  if (id != INV_TIMER) { // create new timer
423  QObject::killTimer(id); // restart timer
424  id = QObject::startTimer(msec);
425  }
426 }
427 
void timerEvent(QTimerEvent *)
Reimplemented Function
Definition: qtimer.cpp:296
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
void setSingleShot(bool singleShot)
Definition: qtimer.h:108
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QSingleShotTimer(int msec, QObject *r, const char *m)
Definition: qtimer.cpp:318
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
uint nulltimer
Definition: qtimer.h:105
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
QTimer(QObject *parent=0)
Constructs a timer with the given parent.
Definition: qtimer.cpp:147
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int timerId() const
Returns the ID of the timer if the timer is running; otherwise returns -1.
Definition: qtimer.h:70
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
const char * name
#define emit
Definition: qobjectdefs.h:76
Q_CORE_EXPORT void qWarning(const char *,...)
void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
Definition: qtimer.cpp:331
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
void timeout()
This signal is emitted when the timer times out.
static const int INV_TIMER
Definition: qtimer.cpp:141
#define Q_OBJECT
Definition: qobjectdefs.h:157
void setInterval(int msec)
Definition: qtimer.cpp:419
int id
Definition: qtimer.h:103
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void qDeleteInEventHandler(QObject *o)
Definition: qobject.cpp:4348
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
~QTimer()
Destroys the timer.
Definition: qtimer.cpp:169
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
bool singleShot
This static function calls a slot after a given time interval.
Definition: qtimer.h:59
int inter
Definition: qtimer.h:103
static QByteArray methodName(const char *signature, int nameLength)
Makes a deep copy of the first nameLength characters of the given method signature and returns the co...
void start()
Definition: qtimer.cpp:231
uint single
Definition: qtimer.h:104
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
void stop()
Stops the timer.
Definition: qtimer.cpp:284
The QAbstractEventDispatcher class provides an interface to manage Qt&#39;s event queue.
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650