I use BeagleBoard x15 to show a counter. When I click a button, it will run a loop:
我使用BeagleBoard x15来显示计数器。当我单击一个按钮时,它将运行一个循环:
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.
但它只显示了最后一个数字:标签上的'9'。它应该显示:每1s后1,2,3,4,5,... 9。我用Qt。
Can you tell me what happen? Thank you.
你能告诉我发生了什么吗?谢谢。
1 个解决方案
#1
0
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
:
问题是因为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);
#1
0
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
:
问题是因为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);