BeagleBoard x15 仅显示最后一个计数器编号? Qt

BeagleBoard x15 shows only the last counter number? Qt

我使用 BeagleBoard x15 来显示计数器。当我点击一个按钮时,它会 运行 一个循环:

for(int i=0;i<10;i++){
    ui->label->setText(QString::number(i));
    delay(1s);
}

但它只显示最后一个数字:标签上的“9”。它应该显示:每 1 秒后显示 1、2、3、4、5、...9。我使用Qt。

你能告诉我发生了什么事吗?谢谢。

这个问题是因为Qt的应用程序在你的延迟阻塞的循环中,正确的做法是使用QTimer:

ui->label->setText("0");
QTimer *timer(this);
connect(timer, &QTimer::timeout, [timer, this](){
    int i = ui->label->text().toInt();
    ui->label->setText(QString::number(i+1));
    if(i == 9)
        timer->stop();
});

timer->start(1000);