ntl381281541的个人空间 https://blog.eetop.cn/510998 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

QT里定时器QTimer的用法

已有 11877 次阅读| 2011-1-11 13:48

来此我的sina博客

 

QTimer Class Reference
[QtCore module]

用这三条语就可以实现定时器每time秒触发一次,直到用timer->stop()槽时才停止触发,要两个值得注意的槽函数而不是信号函数,timer->start(int time),每隔time秒重起一次定时器,timer->stop()取消定时,下面的代码可以循环触发,但若加一句代码timer->setSingleShot(true)将会只启动定时器一次!

QTimer *timer = new QTimer(this);

注销掉此行就可以连续定时timer->setSingleShot(true)
       connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
     timer->start(time*1000);

 

 

 

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

 #include <QTimer>

Inherits QObject.

Properties

Public Functions

  • 29 public functions inherited from QObject

Public Slots

  • 1 public slot inherited from QObject

Signals

Static Public Members

  • void singleShot ( int msec, QObject * receiver, const char * member )
  • 5 static public members inherited from QObject

Additional Inherited Members

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

 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000);

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:

 QTimer::singleShot(200, this, SLOT(updateCaption()));

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

 QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing())); timer->start();

processOneThing() 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 QThreads.

Note that QTimer's accuracy depends on the underlying operating system and hardware. Most platforms support an accuracy of 1 millisecond, but Windows 98 supports only 55. If Qt is unable to deliver the requested number 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() 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 to using QTimer is to use 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

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

This property was introduced in Qt 4.3.

Access functions:

  • bool isActive () const

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.

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().


Member Function Documentation

QTimer::QTimer ( QObject * parent = 0 )

Constructs a timer with the given parent.

QTimer::~QTimer ()

Destroys the timer.

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

Example:

 #include <QApplication> #include <QTimer> int main(int argc, char *argv[]) { QApplication app(argc, argv); QTimer::singleShot(600000, &app, SLOT(quit())); ... return app.exec(); }

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

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().

void QTimer::start ( int msec  [slot]

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

void QTimer::start ()   [slot]

This is an overloaded member function, provided for convenience.

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

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

void QTimer::stop ()   [slot]

Stops the timer.

See also start().

void QTimer::timeout ()   [signal]

This signal is emitted when the timer times out.

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

int QTimer::timerId () const


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 1

    评论
  • 184

    访问数
关闭

站长推荐 上一条 /2 下一条


小黑屋| 手机版| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-11-22 03:41 , Processed in 0.014314 second(s), 6 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部