Timer events and random numbers (sample code)

1. The QTimeerEvent class is used to describe a timer event. For a subclass of QObject, you only need to use the ingQobject:: startTimer(int interval) function to start a timer. This function needs to input a certificate in milliseconds as a parameter to indicate the set event, and the function returns an integer The label to represent this timer. When this timer overflows, you can perform the required operations in the timerEvent() function

id1 = startTimer ( 1000 );  

void Widget::timerEvent(QTimerEvent*event)

{

    if (event->timerId() == id1 ){ // Determine which timer it is      

        qDebug() << "timer1";

    }

    else if (event->timerId() == id2) {

        qDebug() << "timer2";

    }

    else {

        qDebug() << "timer3";

    }

}

2. The QTimer class implements a timer, which provides higher-level programming interfaces, such as signals and slots, and can also set a timer that runs once. (take the signal slot)

 QTimer *timer = new QTimer ( this ); //Create a new timer          

    // Associate the overflow signal of the timer to the slot

    connect(timer, &QTimer::timeout, this,&Widget::timerUpdate);

    timer->start( 1000 ); // Set the overflow time to 1 second and start the timer                        

 

 

 

3. Regarding random numbers, QT is implemented by qrand and qsrand

  int rand = qrand() % 300 ; // Generate a positive integer within 300            


 
 
//.h file
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; int id1, id2, id3; protected: void timerEvent(QTimerEvent *event); private slots: void timerUpdate(); }; #endif // WIDGET_H
 
 
//.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> #include <QTimer> #include <QTime> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui:: Widget) { ui->setupUi(this); id1 = startTimer(1000); // Start a 1-second timer and return its ID id2 = startTimer(1500); id3 = startTimer(2200); QTimer *timer = new QTimer (this); // Create a new timer // Associate the overflow signal of the timer to the slot connect(timer, &QTimer::timeout, this, &Widget::timerUpdate); timer->start(1000); // Set the overflow time to 1 second and start the timer qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); QTimer::singleShot(10000, this, &Widget::close); } Widget ::~Widget() { delete ui; } void Widget::timerEvent(QTimerEvent *event) { if (event->timerId() == id1) { // Determine which timer it is qDebug() << "timer1" ; } else if (event->timerId() == id2) { qDebug() << "timer2";} else if (event->timerId() == id3){ qDebug() << "timer3"; }else{ qDebug()<<"time4"; } } void Widget::timerUpdate() // timer overflow Process { QTime time = QTime::currentTime(); // Get current time QString text = time.toString("hh:mm"); // Convert to string if((time.second() % 2) == 0) text[2]=' '; // Display ":" as a space every second ui->lcdNumber->display(text); int rand = qrand() % 300; // Generate within 300 Positive integer ui->lcdNumber->move(rand, rand); }// Generate a positive integer within 300 ui->lcdNumber->move(rand, rand); }// Generate a positive integer within 300 ui->lcdNumber->move(rand, rand); }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651065&siteId=291194637