Two kinds of timer usage in QT

Source: Internet
Author: User

There are two ways to use timers in Qt, one is to use the timer of 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.

timer for 1.QObject class

Qobject is the base class for all Qt objects, and it provides a basic timer. With Qobject::starttimer (), you can start the timer with a single millisecond interval as a parameter, which returns the identifier of a unique integer timer. The timer begins to "trigger" at every interval until explicitly using the timer's identifier to call Qobject::killtimer () end.

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

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

Intqobject::starttimer (int interval);

Start a timer and return the timer ID, and if you can't start a timer, it will return 0. After the timer starts, a timeout event is triggered every interval millisecond interval until KillTimer () is called to remove the timer. If the interval is 0, then the timer event is not handled by the window System event each time it 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 manipulate it.

When the timer event occurs, the virtual function timerevent () is invoked along with the Qtimerevent event argument class, which can be used to obtain the timer event.

The use of the timer is as follows:

Header file

Class Qnewobject:publicqobject

{

Q_object

Public

Qnewobject (QObject * parent = 0);

Virtual ~qnewobject ();

Protected

void TimerEvent (Qtimerevent *event);

int M_ntimerid;

};

source files

Qnewobject::qnewobject (QObject * parent)

: Qnewobject (parent)

{

M_ntimerid = Starttimer (1000);

}

Qnewobject::~qnewobject ()

{

if (m_ntimerid!= 0)

KillTimer (M_ntimerid);

}

Voidqnewobject::timerevent (qtimerevent *event)

{

Qdebug ("Timer event, id%d", Event->timerid ());

}

2. Timer class Qtimer

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

Creating timers

Qtimer *testtimer = Newqtimer (this);

Connect the timer timeout signal to the slot (functional function)

Connect (testtimer,signal (timeout ()), this, SLOT (TestFunction ()));

Start running timer, timed interval is 1000ms

Testtimer->start (1000);

...

Stop running the timer

if (Testtimer->isactive ())

Testtimer->stop ();

Qtimer also provides a simple, once timed function singleshot (). A timer triggers the handler function animatetimeout () after 100ms and triggers only once. The code is as follows:

Qtimer::singleshot (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 versatile timer. Qtimer is easy to use: Create a Qtimer, start by using Start () and connect its timeout () to the appropriate slot. When this time has passed, it will emit timeout () signals.

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

Instance:

Qtimer *timer = new Qtimer (myObject);

Connect (timer, SIGNAL (timeout ()), MyObject, SLOT (Timerdone ()));

Timer->start (TRUE); 2 seconds Single Trigger timer

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 timer of 0 Qtimer will be time.

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

Qtimer *t = new Qtimer (myObject);

Connect (t, SIGNAL (timeout ()), SLOT (Processonething ()));

T->start (0, FALSE);

Myobject->processonething () will be called repeatedly and should be returned soon (usually after a data item is processed) so that QT can pass the event on to the widget and stop the timer once it completes the work. This is a typical way to do heavy work in a graphical user interface application, and now multithreading can be used on more and more platforms, and we hope that invalid events will eventually be replaced by threads.

Note that the accuracy of Qtimer depends on the operating system and hardware underneath. Most platforms support 20 milliseconds of precision, and some platforms can provide higher. If QT cannot transmit the number required by the timer trigger, it will silently discard some.

Another way to use Qtimer is to call Qobject::starttimer () for your object and to implement Qobject::timerevent () event handlers in your class (and of course you must inherit Qobject). The disadvantage is that timerevent () does not support advanced levels like a single trigger timer or signal.

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

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.