The Qtimer of QT

Source: Internet
Author: User
Tags emit setinterval time interval
Brief Introduction


The Qtimer class provides a timer for repetitive and single-shot triggering signals.



The Qtimer class provides a high-level programming interface for timers. Easy to use: First, create a qtimer, connect the timeout () signal to the appropriate slot function, call Start (), and then emit a timeout () signal at a constant interval.



Note: When the parent object of Qtimer is destroyed, it is also automatically destroyed.



Brief description Precision Substitution Qtimer member function Signal example effect source more reference



Detailed Description



In the QT analog clock, 1 seconds (1000 milliseconds) are updated:


Qtimer *timer = new Qtimer (this);
Connect (timer, SIGNAL (), this, SLOT (update ()));
Timer->start (1000);


After start (), update () is called every second.



You can make the timer execute only once by setting Setsingleshot (true). You can also use the static function Qtimer::singleshot ():


Qtimer::singleshot (A, this, SLOT (Updatecaption ()));


In a multithreaded program, you can use Qtimer in any thread that has an event loop. Use Qthread::exec () to start an event loop from a non-GUI thread. QT uses the thread Association of the timer to determine which thread will emit a timeout () signal. Because of this, you must start and stop the timer in its thread, and it is not possible to start the timer from another thread.



As a special case, once all the events in the Window System event queue have been processed, a qtimer with a timing of 0 will be up to the time. When you need to provide a smooth user interface, you can use this to do more heavy work.


Qtimer *timer = new Qtimer (this);
Connect (timer, SIGNAL (), this, SLOT (Processonething ()));
Timer->start ();


At this point, 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 0-millisecond Qtimer object to eventually be replaced by threads. 

accuracy



The accuracy of the timer depends on the underlying operating system and hardware. The majority of platform support accuracy is 1 milliseconds, although the accuracy of the timer in many real-world situations and this does not match.



Accuracy also depends on the timer type (qt::timertype). For QT::P Recisetimer, Qtimer will try to maintain accuracy in 1 milliseconds. The precise timer never expires sooner than expected.



For Qt::coarsetimer and Qt::verycoarsetimer types, Qtimer may be woken up earlier than expected: The 5%,qt::verycoarsetimer of Qt::coarsetimer interval is 500 milliseconds.



Enumeration Qt::timertype:


Constants value Description
Qt::P Recisetimer 0 Accurate timers to keep the millisecond accuracy as far as possible.
Qt::coarsetimer 1 The coarse timer, try to keep the accuracy within the required interval of 5% in the range.
Qt::verycoarsetimer 2 A very coarse timer that retains only the complete second precision.


In Unix (include: Linux, OS X, IOS), QT will be QT::P Recisetimer to maintain millisecond accuracy, for qt::coarsetimer, the interval will be adjusted to 5%, so that the timer matches with other timers or at almost the same time, The goal is to have most timers wake up at the same time, thereby reducing CPU wake-up and power consumption.



On Windows, QT will be QT::P Recisetimer uses the Windows Multimedia Timer tool (if available) to use the normal Windows timers for Qt::coarsetimer and Qt::verycoarsetimer.



On all platforms, the interval of Qt::verycoarsetimer is rounded to the nearest full second bit (for example: 23500ms of time interval will be rounded to 24000ms,20300ms rounded to 20000). 

Alternative Qtimer



Another way to use Qtimer: Call Qobject::starttimer () for your object and re-implement the Qobject::timerevent () event handler in your class (you must inherit Qobject). The disadvantage is that timerevent () does not support advanced features such as a single trigger timer or signal.



Another option is Qbasictimer. It is usually more direct than using Qobject::starttimer (). You can view the three ways that timers describes in the assistant.



Some operating system restrictions may limit the number of timers, and QT will try to work within limits.



Refer to: Qbasictimer, Qtimerevent, Qobject::timerevent (), timers, Analog Clock Example, wiggly Example.

member functions



BOOL IsActive () const
Returns true if the timer is running, otherwise false is returned.



int Remainingtime () const
Returns the remaining time (in milliseconds) of the timer until it expires.



If the timer is inactive, the return value is-1. If the timer expires, the return value is 0.



void setinterval (int msec)
Sets the time-out interval (in milliseconds).



The default value is 0, and once all events in the Window System event queue have been processed, a qtimer with an interval of 0 will be triggered.



void Setsingleshot (bool singleshot)
Sets whether the timer is a single-shot trigger.



A single trigger timer is triggered only once, not once, and every time interval is triggered.



void Settimertype (Qt::timertype atype)
Set the accuracy of the timer. The default value is Qt::coarsetimer.



int Timerid () const
If the timer is running, return the ID of the timer, otherwise return-1.



void start (int msec)
Start or restart a timer that has a time-out interval of milliseconds.



If the timer is running, it will be stopped and restarted. If Singleshot is true, the timer will be activated only once.



void Start ()
Above, the start () is overloaded.



void Stop ()
Stop the timer.

  Signal



void timeout ()
After the timer expires, the signal is fired.



Note: This is a private signal. It can be used in a signal connection, but cannot be emitted by the user. 

Example



Below, we take qtimer as an example, using the start and stop buttons to manipulate the update of a progress bar. 

effect



Source Code


qpushbutton *pstartbutton = new Qpushbutton (this);
Qpushbutton *pstopbutton = new Qpushbutton (this);
M_pprogressbar = new Qprogressbar (this);

M_ptimer = new Qtimer ();
Pstartbutton->settext (Qstring::fromlocal8bit ("Start"));

Pstopbutton->settext (Qstring::fromlocal8bit ("Stop"));
M_pprogressbar->setrange (0, 100);

M_pprogressbar->setvalue (50);

Set timeout interval m_ptimer->setinterval (1000);
Connect the signal slot connect (Pstartbutton, SIGNAL (clicked (bool)), M_ptimer, slot (Start ()));
Connect (Pstopbutton, SIGNAL (clicked (bool)), M_ptimer, SLOT (Stop ()));

Connect (M_ptimer, SIGNAL (timeout ()), this, SLOT (UpdateProgress ()));
    slot function void Mainwindow::updateprogress () {//Get current progress value, +1 int ncurrentvalue = M_pprogressbar->value ();
    ncurrentvalue++;

    if (Ncurrentvalue >=) m_ptimer->stop ();
Set a new progress value of M_pprogressbar->setvalue (Ncurrentvalue); }


In the Slot function UpdateProgress (), first through M_pprogressbar->value () to get the current progress value, and then add 1, when the progress is greater than or equal to 100 stop the timer (and then continue to do not have any meaning, Because the progress has reached 100 and the resource is not stopped, then set the value of the progress bar. 

More Reference

QT Analog clock


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.