Qt timer and random number details

  • 2020-04-02 03:06:18
  • OfStack

The environment is Windows 7 +Qt 4.8.1 +Qt Creator 2.4.1

One, timer

There are two ways to use timers in Qt: one is to use timer events, and the other is to use signals and slots. Generally, it is best to use timer events when using more than one timer.

1. Create a new Qt Gui application, the project name is myTimer, the base class selects QWidget, the class name is Widget.

2. Add the function declaration to the widget.h file:
Protected:
      Void timerEvent (QTimerEvent *);
Then add the private variable definition:
Int id1, id2, id3;

3. Go to design mode and drag two Label parts Label onto the interface.

4. Now enter the widget.cpp file and add the following code in the constructor:


id1 = startTimer(1000); //Start a one-second timer and return its ID
id2 = startTimer(2000);
id3 = startTimer(10000);

Three timers are started, and each returns their id, which is used to distinguish the different timers. The timer is measured in milliseconds. Each time a timer overflows, the timer event handler is called, and we can handle it in this function.

5. Add the definition of the timer event handler function as follows:


void Widget::timerEvent(QTimerEvent *event)
{
  if (event->timerId() == id1) {    //Determine which timer it is
    ui->label->setText(tr("%1").arg(qrand()%10));
  }
  else if (event->timerId() == id2) {
    ui->label_2->setText(tr("hello world!"));
  }
  else {
    qApp->quit();
  }
}

Here, the id of the overflow timer is returned using the timerId() function, and then the id is used to determine which timer overflowed and handle it accordingly. Every time the first timer overflows, a random number less than 10 is generated. When the second timer overflows, the text of the label is changed. When the third timer overflows, the application exits. Now you can run the program and see what it looks like.

6. If you just want to start a small number of timers, you can also use signals and slots.
Add a private slot declaration in widget.h:


private slots:
  void timerUpdate();

Then go to design mode and add a Line Edit to the interface, then add a header file to widget.cpp that contains:


#include <QTimer>
#include <QDateTime>

Then add the following code to the constructor:


QTimer *timer = new QTimer(this);
//Associate the timer overflow signal with the corresponding slot function
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);

A timer is created, its overflow signal is associated with the update slot, and the timer is started using the start() function.
Now add the definition of the timerUpdate() function:


void Widget::timerUpdate()
{
  //Gets the current time of the system
  QDateTime time = QDateTime::currentDateTime();
  //Set the system time display format
  QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
  //Displays the time on the label
  ui->lineEdit->setText(str);
}

      The current time is shown here in the line editor. Now you can run the program and see what it looks like.

Random Numbers

Regarding random Numbers, qrand() and qsrand() functions are used in Qt. You've seen the use of the qrand() function in the previous program to generate random Numbers, and qrand()%10 to generate random Numbers between 0 and 9. To generate random Numbers up to 100 is %100. And so on.
Before using the qrand() function to generate random Numbers, the qsrand() function is usually used to set the initial value. If the initial value is not set, qrand() will generate the same set of random Numbers each time the program is run. To generate a different random number each time we run the program, we use qsrand() to set a different initial value. This USES the secsTo() function of the QTime class, which represents the number of seconds between two points in time, such as the number of seconds from zero to the current time in the code.

Let's first add the following code to the constructor of widget.cpp:


qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

Then add the following code at the end of the timerUpdate() function:


int rand = qrand() % 300;      //Produces positive integers up to 300
ui->lineEdit->move(rand, rand);

So, every second, the line editor moves to a random position. You can run the program and see what it looks like.

The above is all the content of this article, I hope you can enjoy it.


Related articles: