QT里定时器QTimer的用法

QTimer Class Reference
[QtCoremodule]

用这三条语就可以实现定时器每time秒触发一次,直到用timer->stop()槽时才停止触发,要两个值得注意的槽函数而不是信号函数,timer->start(inttime),每隔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

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 fortimers. To use it, create a QTimer, connect its timeout()signal to the appropriate slots, and call start(). Fromthen on it will emit the timeout()signal at constant intervals.

Example for a one second (1000 millisecond) timer (from theAnalogClock example):

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

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

You can set a timer to time out only once by callingsetSingleShot(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 threadthat has an event loop. To start an event loop from a non-GUIthread, use QThread::exec().Qt uses the the timer's threadaffinity to determine which thread will emit the timeout()signal. Because of this, you must start and stop the timer in itsthread; it is not possible to start a timer from anotherthread.

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

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

processOneThing() will from then on be calledrepeatedly. It should be written in such a way that it alwaysreturns 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 of implementingheavy work in GUI applications; multithreading is now becomingavailable on more and more platforms, and we expect thatzero-millisecond QTimers will gradually be replaced by QThreads.

Note that QTimer's accuracy depends on the underlying operatingsystem and hardware. Most platforms support an accuracy of 1millisecond, but Windows 98 supports only 55. If Qt is unable todeliver the requested number of timer clicks, it will silentlydiscard 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). Thedisadvantage is that timerEvent()does not support such high-level features as single-shot timers orsignals.

Another alternative to using QTimer is to use QBasicTimer. Itis typically less cumbersome than using QObject::startTimer()directly. See Timers for anoverview of all three approaches.

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

See also QBasicTimer,QTimerEvent,QObject::timerEvent(),Timers,AnalogClock Example, and WigglyExample.


Property Documentation

active : constbool

This boolean property is true if the timer is running; otherwisefalse.

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 atimeout interval of 0 will time out as soon as all the events inthe 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-shottimer.

A single-shot timer fires only once, non-single-shot timers fireevery intervalmilliseconds.

Access functions:

  • bool isSingleShot () const
  • void setSingleShot ( bool singleShot )

See also intervaland singleShot().


Member Function Documentation

QTimer::QTimer ( QObject *parent = 0 )

Constructs a timer with the given parent.

QTimer::~QTimer()

Destroys the timer.

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

This static function calls a slot after a given timeinterval.

It is very convenient to use this function because you do notneed to bother with a timerEventor 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 themember is the slot. The time interval is msecmilliseconds.

Note: This function is reentrant.

See also setSingleShot()and start().

void QTimer::start ( intmsec )  [slot]

Starts or restarts the timer with a timeout interval ofmsec milliseconds.

void QTimer::start()   [slot]

This is an overloaded member function, provided forconvenience.

Starts or restarts the timer with the timeout specified ininterval.

If singleShotis 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(), andstop().

int QTimer::timerId ()const


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值