1 定时器 1.1 定时器事件 void timerEvent( QTimerEvent * e) 1.2 启动定时器 id1 = startTimer(毫秒) 1.3 判断具体定时器标准 e->timerId() == id1
[C++] 纯文本查看 复制代码 //开启定时器
id1 = startTimer(1000); //单位 毫秒
id2 = startTimer(2000); //单位 毫秒
//添加定时器事件
void Widget::timerEvent(QTimerEvent * e)
{
if(e->timerId() == id1)
{
//每隔1秒中 让label_2数字++
static int num = 1;
ui->label_2->setText( QString::number(num++) );
}
if(e->timerId() == id2)
{
//每隔2秒让label_3数字++
static int num2 = 1;
ui->label_3->setText( QString::number(num2++) );
}
}
通过定时器类实现 (推荐) 2.2 创建定时器对象 2.2.1 QTimer * timer1 = new QTimer(this); 2.3 开启定时器 2.3.1 timer1->start(x毫秒) 2.4 每隔x毫秒 会抛出一个信号timeout出来
2.5 监听信号处理逻辑 2.6 暂停定时器 timer1->stop();
[C++] 纯文本查看 复制代码 QTimer * timer1 = new QTimer(this);
timer1->start(500);
//每隔0.5秒 会抛出一个信号timeout出来
connect(timer1,&QTimer::timeout , [=](){
//每隔0.5秒中 让label_4数字++
static int num = 1;
ui->label_4->setText( QString::number(num++) );
});
//点击暂停按钮 停止定时器
connect(ui->pushButton,&QPushButton::clicked,[=](){
timer1->stop();
|