QT Learning Timer

Source: Internet
Author: User

There are two ways to use timers in Qt, one is to use the Qobiect class, and one is to use the Qtimer class. The accuracy of the timer depends on the operating system and hardware, and most platforms support 20ms accuracy.

Timers for the Qobject class

Qobject is the base class for all Qt objects, and it provides a basic timer. by Qobject::starttimer (), a one-millisecond interval can be used as a parameter to start the timer, which returns the identifier of a unique integer timer. This timer starts at each interval "trigger" until the end of the Qobject::killtimer () is explicitly invoked using the identifier of the timer.

When the timer is triggered, the application sends a qtimerevent. In the event loop, the processor handles timer events in the order of the event queues. When the processor is busy with other event processing, the timer cannot be processed immediately.

The Qobject class also provides time-definite functionality. Timer-related member functions are: Starttimer (), Timeevent (), KillTimer (). The Starttimer () and TimerEvent () prototypes in the Qobject base class are described below:

intQObject::startTimer(int interval);

Starts a timer and returns the timer ID, and returns 0 if a timer cannot be started. Once the timer starts, a timeout event is triggered every interval millisecond interval until KillTimer () is called to remove the timer. If interval is 0, there is no window System event handling for each time the timer event occurs.

virtual voidQObject::timerEvent(QTimerEvent *event);

The virtual function timerevent () is overloaded to implement the user's Timeout event handler function. If more than one timer is running, Qtimerevent::timerid () is used to find the specified timer and operate on it.

When the timer event occurs, the virtual function timerevent () is called along with the Qtimerevent event parameter class, which overloads the function to obtain the timer event.

The usage of the timer is as follows:

//header file  class Qnewobject: publicqobject{q_objectpublic : qnewobject  (    Qobject * parent = 0 ); virtual  ~qnewobject (); protected : void      TimerEvent  (qtimerevent *event ); int  M_ntimerid;};  
//源文件parent )    parent ){    m_nTimerId = startTimer(1000);}QNewObject::~QNewObject(){    if0 )        *event ){    "timer event, id %d",event->timerId() );}
Timer class Qtimer

The Timer class Qtimer provides a timer that emits a signal when the timer is triggered, and he provides a timeout event that is triggered only once, usually using the following method:

//创建定时器QTimer *testTimer = newQTimer(this);//将定时器超时信号与槽(功能函数)联系起来connect( testTimer,SIGNAL(timeout()), this, SLOT(testFunction()) );//开始运行定时器,定时时间间隔为1000mstestTimer->start(1000);...//停止运行定时器if (testTimer->isActive() )    testTimer->stop();

Qtimer also provides a simple function singleshot () with only one timer. A timer triggers the processing function animatetimeout () after 100ms and fires only once. The code is as follows:

100,this, SLOT(animateTimeout()) );

The Qtimer class provides a timer signal and a single trigger timer.

It uses timer events internally to provide a more general-purpose timer. Qtimer is easy to use: Create a Qtimer, use Start () to start and connect its timeout () to the appropriate slot. When this time passes, it will emit a timeout () signal.

Note that when the parent object of Qtimer is destroyed, it is also automatically destroyed.

Instance:

*=new QTimer( myObject );        connect( timer, SIGNAL(timeout()),myObject, SLOT(timerDone()) );        timer->2000TRUE// 2秒单触发定时器

You can also use the static Singleshot () function to create a single trigger timer.

As a special case, once all the events in the Window System event queue have been processed, a qtimer of 0 will be up to the time.

This can also be used when providing a quick user interface to do more heavy work.

*=new QTimer( myObject );        connect( t, SIGNAL(timeout()), SLOT(processOneThing()));        t->0FALSE );

Myobject->processonething () will be repeated and should be returned very quickly (usually after processing a data item) so that QT can send the event to the widget and stop the timer once it finishes the job. This is a typical way of doing a lot of work in a graphical user interface application, and now multithreading can be used on more and more platforms, and we want the invalid events to eventually be replaced by threads.

Note that the accuracy of the qtimer depends on the operating system and hardware underneath. Most platforms support 20 milliseconds of accuracy, and some platforms can provide higher. If QT does not transmit the required number of timer triggers, it will silently discard some.

Another way to use Qtimer is to call Qobject::starttimer () for your object and re-implement the Qobject::timerevent () event handler in your class (which must inherit qobject, of course). The disadvantage is that timerevent () does not support advanced levels such as single-trigger timers or signals.

Some operating systems limit the number of timers that may be used, and QT will try to work within the limits.

QT Learning Timer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.