How to use the timer in QT

Source: Internet
Author: User
Tags time interval
How to use the timer in QTCategory: qt 2005-08-21 12:27 3823 people read reviews (3) Favorites report How to use timers in Qt
(1) overload the TimerEvent (Qtimerevent *) function, and then set the time interval in the class's constructor
Starttimer (50);//Unit is milliseconds
(2) Set the following in the constructor of the class:
Qtimer *timer=new Qtimer (this);
Connect (timer,signal (timeout ()), This,slot (Timeoutslot ())),//timeoutslot () is a custom slot
Timer->start (1000);


Two applications of QT timer 2009-10-14 8:44

Can be implemented with slot functions

(1) overload the TimerEvent (Qtimerevent *) function, and then set the time interval in the class's constructor
Starttimer (50);//Unit is milliseconds
(2) Set the following in the constructor of the class: Qtimer *timer=new qtimer(this); Connect (timer,signal (), this, slot(Timeoutslot ())),//timeoutslot () is the custom slot Timer->start (1000);

Qtimer *timer=new Qtimer (this);
   Connect (timer,signal (), This,slot (Timeoutslot ())),//timeoutslot () is the custom slot
   Timer->start (1000);

However: all Qobject subclasses do not have to load a Qtimer object when setting the timer, because it is inconvenient to waste resources and to write unnecessary functions. The best way is to overload the TimerEvent function, as follows: class Gui _dlgviewctrldatum: public Qdialog {q_object public: gui_dlgviewctrldatum (qwidget* parent = 0, con St char* name = 0, bool modal = FALSE, wflags fl = 0); ~gui_dlgviewctrldatum (); protected: void timerevent (qtimerevent *); }; void Gui_dlgviewctrldatum::timerevent (qtimerevent *e) {//statements}

Class Gui_dlgviewctrldatum:public Qdialog
{
q_object public
:
gui_dlgviewctrldatum (qwidget* Parent = 0, const char* name = 0, bool modal = FALSE, wflags fl = 0);
~gui_dlgviewctrldatum ();
Protected:
void TimerEvent (Qtimerevent *);
};
void Gui_dlgviewctrldatum::timerevent (qtimerevent *e)
{
//statements
}

Then set the time interval in the Gui_dlgviewctrldatum constructor:
Starttimer (50);//Unit is milliseconds

In this way, the function timerevent is called once every 50 milliseconds.

Online also said:

The priority of the timer event is very low, and if multiple timers are required, it is time-consuming to track the ID of each timer. In this case, a better approach is to create a Qtimer object for each timer. At each time interval, the Qtimer emits a timeout () signal. The Qtimer also supports a one-time timer (a timer that emits a timeout () signal only once).

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.