QT timer QTimer

Timer is used to handle an object-recurring events, similar to a hardware timer. Such as setting a timer in a period of 1000 ms, 1000 ms then each will transmit timer timeout () signal, the signal associated with the function in the slot's corresponding processing can be done.

Qt timer in the class is QTimer. QTimer not a visible interface components, can not find it in the Palette UI designer's. Examples of the program in Figure 1 implements a timer function, the timer is started to calculate the length of time stopped, the timer is QTime class.



FIG 1 a schematic running timer instance


QTimer interval The main property is, is the period of time interrupt milliseconds. The main signal is QTimer timeout (), transmit this signal when the timer interrupt, in order to respond to the timer interrupt, the need to write this timeout () function of the signal slot. The following is the increased window class definitions (omitting other irrelevant defined):

 
  1. class Dialog : public QDialog
  2. {
  3. private:
  4. QTimer * fTimer; // timer
  5. QTime fTimeCounter; // timer
  6. private slots:
  7. void on_timer_timeout (); // time-out function processing tank
  8. };

This defines a timer fTimer, - a timer fTimeCounter. Also defines a slot function on_timer_ timeout (), a timer to timeout () signal in response to the slot function.

We need to create a timer in the constructor's window class, and the associated signals and slots. code show as below:

 
  1. Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
  2. {
  3. ui-> setupUi (this);
  4. fTimer=new QTimer(this);
  5. fTimer->stop();
  6. fTimer-> setInterval (1000); // set the timer period, unit: ms
  7. connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
  8. }

Slot function on_timer_timeout () of the codes are as follows:

 
  1. void Dialog::on_timer_timeout()
  2. {
  3. // timer interrupt ring
  4. QTime curTime = QTime :: currentTime (); // Get the current time
  5. ui-> LCDHour-> display (curTime.hour ()); // display h
  6. ui-> LCDMin-> display (curTime.minute ()); // show min
  7. ui-> LCDSec-> display (curTime.second ()); // second display
  8. int va=ui->progressBar->value();
  9. will ++;
  10. if (va>100)
  11. A = 0;
  12. ui->progressBar->setValue(va);
  13. }

This code first gets a QTime class static function Qtime :: currentTime () the current time, and then were acquired in hours, minutes, seconds, hour QTime member function of (), minute (), second (), and in a few LCDNumber the assembly is not significant. ProgressBar update cycle value, and in order to make the interface changes to indicate timer is running.

Set the timer period, simply call Qtimer :: setlnterval () function can be.

QTimer :: start () function is used to start the timer, the "Start" button on the interface code is as follows:

 
  1. void Dialog::on_btnStart_clicked()
  2. {
  3. fTimer-> start (); // timer starts operation
  4. fTimeCounter.start (); // timer starts work
  5. ui->btnStart->setEnabled(false);
  6. ui->btnStop->setEnabled(true);
  7. ui->btnSetIntv->setEnabled(false);
  8. }

The timer fTimeCounter execution start () is the current time as a timer of time.

QTimer :: Stop () function stops the timer, the "stop" button on the interface to achieve this function, its code is as follows:

  1. void Dialog::on_btnStop_clicked()
  2. {
  3. fTimer-> stop (); // timer stops
  4. int tmMsec = fTimeCounter.elapsed (); // number of milliseconds
  5. int ms=tmMsec%1000;
  6. int sec=tmMsec/1000;
  7. QString str = QString :: asprintf ( "elapsed time:% d seconds,% d ms", sec, ms);
  8. ui->LabElapsTime->setText(str);
  9. ui->btnStart->setEnabled(true);
  10. ui->btnStop->setEnabled(false);
  11. ui->btnSetIntv->setEnabled(true);
  12. }
Published 21 original articles · won praise 2 · Views 2201

Guess you like

Origin blog.csdn.net/conimade/article/details/104391115