QT Timer and random number detailed _c language

Source: Internet
Author: User
Tags id3 rand

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 for timer events and the other for using signals and slots. It is usually best to use timer events to handle multiple timers.

1. New QT GUI application, project name is MyTimer, base class chooses Qwidget, class name is widget.

2. Add a function declaration to the Widget.h file:
Protected
void TimerEvent (Qtimerevent *);
Then add the private variable definition:
int id1, ID2, ID3;

3. Next to design mode, drag the two label parts label to the interface.

4. Below enter the Widget.cpp file, first add the following code in the constructor:

ID1 = Starttimer (1000); Turn on a 1-second timer, return its id
id2 = starttimer (watts);
ID3 = Starttimer (10000);

Three timers are turned on, and their IDs are returned, which is used to distinguish between different timers. The time unit of the timer is milliseconds. Whenever a timer overflows, the timer event handler is invoked, which we can handle in the function.

5. Add the following definition of the Timer event handler function:

void Widget::timerevent (qtimerevent *event)
{
  if (event->timerid () = = ID1) {    //judge which timer 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 we first use the Timerid () function to return the ID of the overflow timer, and then according to the ID to determine which timer overflowed and handle accordingly. A random number less than 10 is generated whenever the first timer overflows; the text of the label is changed when the second timer overflows, and the application exits when the third timer overflows. You can now run the program to see the effect.

6. If you just want to open a small number of timers, you can also use signals and slots to achieve.
First, add a private slot declaration to the widget.h:

Private slots:
  void Timerupdate ();

Then in design mode, add a row editor part line edit to the interface and add a header file to Widget.cpp to include:

#include <QTimer>
#include <QDateTime>

Then add the following code to the constructor:

Qtimer *timer = new Qtimer (this);
Associated timer overflow signal and corresponding slot function
Connect (timer, SIGNAL (timeout ()), this, SLOT (Timerupdate ()));
Timer->start (1000);

This creates a timer, associates its overflow signal with the update slot, and finally uses the start () function to open the timer.
Add the definition of the timerupdate () function below:

void Widget::timerupdate ()
{
  //Get the system now
  qdatetime time = Qdatetime::currentdatetime ();
  Set the system time display format
  QString str = time.tostring ("Yyyy-mm-dd hh:mm:ss dddd");
  Show Time
  ui->lineedit->settext (str) on the label;
}

This shows the current time in the row editor. You can now run the program to see the effect.

Second, random number

With respect to random numbers, it is implemented using the Qrand () and Qsrand () two functions in Qt. The use of the Qrand () function has been seen in the previous program, which produces random numbers, Qrand ()%10 can produce random numbers between 0-9. To produce a random number within 100 is%100. Analogy
Before using the Qrand () function to generate a random number, you typically use the Qsrand () function to set an initial value for it, and if you do not set an initial value, the Qrand () will produce the same set of random numbers each time the program is run. In order to run the program every time, can produce a different random number, we want to use Qsrand () to set a different initial value. This uses the Secsto () function of the Qtime class, which represents the number of seconds that are contained between two points of time, such as the number of seconds elapsed from 0 o'clock to the current time in the code.

The following code is added to the Widget.cpp constructor first:

Qsrand (qtime (0, 0, 0). Secsto (Qtime::currenttime ()));

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

int rand = Qrand ()%;      Produces a positive integer within 300
ui->lineedit->move (Rand, Rand);

This way, every second, the row editor moves to a random location. Everyone can run the program to see the effect.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.