QT Development (vii)--timer event, System time, random number

Source: Internet
Author: User

Let's learn a little bit of knowledge, is the timer time, system time, as well as random number, here to explain that the event and signal is not the same, do not confuse, the surface appears to be to trigger a function, in fact, you can understand the time he is more inclined to the bottom of some one. Timer Event

We actually learned about the timer QT Development (v)--project combat: Stopwatch, the use of Qtime,qtimer

In this small project, we are using the timeout signal to achieve the timing trigger, in fact, as in the previous article, we have a corresponding event qt, we look at: void TimerEvent (Qtimerevent * event);

It's easy to use.

This->id = Starttimer (1000);

We call Starttimer directly to specify the interval time, the unit is MS, and returns an int, which is an ID

void Mainwindow::timerevent (qtimerevent *event)
{
    if (event->timerid () = = This->id)
    {
        Qdebug ( ) << "trigger";
    }

This will trigger once every 1s, if you want to stop this timer

KillTimer (ID);

Now find out the usefulness of this ID. two. System Time

System time to obtain, you can achieve this

Qdatetime time = Qdatetime::currentdatetime ();
QString str = time.tostring ("Yyyy-mm-dd hh:mm:ss dddd");
This->ui->tv_time->settext (str);

We get to the later conversion format through the Qdatetime static function Currentdatetime, but we get a static value, and if we want to implement a clock, we need to use the timer above, which is what we should do in our timer event:

void Mainwindow::timerevent (qtimerevent *event)
{
    if (event->timerid () = = This->id)
    {
        Qdatetime time = Qdatetime::currentdatetime ();
        QString str = time.tostring ("Yyyy-mm-dd hh:mm:ss dddd");
        This->ui->tv_time->settext (str);
    }

So you can realize the clock

three. Random number

Random numbers on a function qrand (), his formula is Qrand ()% n (n followed by the 0-n interval)

Let's implement a moving small case, with the example above to deal with, we randomly move this time control

void Mainwindow::timerevent (qtimerevent *event)
{
    if (event->timerid () = = This->id)
    {
        Qdatetime time = Qdatetime::currentdatetime ();
        QString str = time.tostring ("Yyyy-mm-dd hh:mm:ss dddd");
        This->ui->tv_time->settext (str);
        int a = Qrand ()%300;
        This->ui->tv_time->move (a,a);
    }

That's going to work.

interested can add group: 690351511

Related Article

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.