I use BeagleBoard x15 to show a counter. When I click a button, it will run a loop:
for(int i=0;i<10;i++){
ui->label->setText(QString::number(i));
delay(1s);
}
But it only showed the last number: '9' on label. It should show: 1, 2, 3, 4, 5,...9 after each 1s. I use Qt.
Can you tell me what happen? Thank you.
The problem is caused because the application of Qt lives in a loop that your delay blocks, the correct thing is to use a 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);