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). |