为什么要用3个定时器分别进行不同的操作?

应用程序输出栏输出的这些东西是按照什么顺序输出的,看不懂
图片说明
图片说明

 #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);
    id2=startTimer(2000);
    id3=startTimer(3000);
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
    timer->start(1000);
}

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId()==id1)
    {
        qDebug()<<"timer1";
    }
    else if(event->timerId()==id2)
    {
        qDebug()<<"timer2";
    }
    else
    {
        qDebug()<<"timer3";
    }
}

void Widget::timerUpdate()
{
    QTime time=QTime::currentTime();
    QString text=time.toString("hh:mm");
    if((time.second()%2)==0)
        text[2]=' ';
    ui->lcdNumber->display(text);
}

Widget::~Widget()
{
    delete ui;
}

id1=startTimer(1000);
id2=startTimer(2000);
id3=startTimer(3000);

定时器时间到了就执行。qt底层使用的是消息,因此谁先触发,谁先执行。三者之间没有同步。

三个定时器基本上同时启动,但定时的时间分别是:1秒、2秒和3秒。
这样三个定时循环产生,会输出如下的日志。

如果你希望三者同步,就不要用3个定时器,而是使用一个1000毫秒间隔的定时器。
并且定义一个全局变量叫count,每次定时器到了,count++
根据以下判断执行:

首先执行原先A定时器的代码
if (count % 2 == 0)执行B的代码
if (count % 3 == 0)执行C的代码