Solved SetText to label before sleep, but it's set after
-
Hello,
I'd like to change a text of a label - then wait some time - change it again - wait again ... etc.
But somehow the label will not set before the sleep function, it is written after it.QString whosturn[3]; whosturn[0] = QString("p1"); whosturn[1] = QString("p2"); whosturn[2] = QString("p3"); whosturn[3] = QString("p4"); for(int i = 1; i<4;i++){ ui->my_label->setText(whosturn[i]); std::this_thread::sleep_for(std::chrono::milliseconds(5000)); }
The setText is set only ONCE after the whole for ...
Thanks in advance -
Text is set exactly the amount of times you set it (you can query it with
text()
if you want to check). The ui just repaints once it gets the chance. The paint event happens when the control gets back to the event loop, but you're blocking the ui thread, so the events are processed only when you exit your loop and let the control go back to Qt.Sleeping in the ui thread is almost always the wrong thing to do.
-
To add to @Chris-Kawa, there's a way to cheat: do a
qApp->processEvents();
just before sleeping. -
@Chris-Kawa said in SetText to label before sleep, but it's set after:
Sleeping in the ui thread is almost always the wrong thing to do.
I'd even venture to add:
Sleeping wherever is almost always the wrong thing to do.