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