sleep() doesn't work in synch with button slots

27 views Asked by At

I am trying to create a function that would display a message for only a few seconds after a button press, before displaying nothing once more.

Changing another label text works just fine, but the problem occurs when I try to make an empty label, say something like:

ui->label->setText("Message from button press")

then call sleep(4);, then again:

ui->label->setText("");

Even stranger, sleep() works just fine with other functions like QApplication::quit().

Is there an alternative method in Qt for waiting a certain amount of time?

Here is the code from a question block, but it seems to have the same result with other button presses.

void Practice::on_question_clicked()
{
    QMessageBox::StandardButton reply = QMessageBox::question(this, "Question", "Do you understand how to program buttons in QT to activate these six types of message boxes?", QMessageBox::Yes | QMessageBox::No);
    if(reply == QMessageBox::Yes)
    {
        ui->output_label->setText("Glad you understand. Aborting program in 4 seconds.");
        sleep(4);
        ui->output_label->setText("");
        QApplication::quit();
    }
    else
    {
        ui->output_label->setText("Keep studying and you'll get it eventually.");
    }
}

What I expected to happen:
After pressing Yes in the message box, the message "Glad you understand. Aborting program in 4 seconds." should have displayed for 4 seconds, then the label would go blank as the application shut down.

What actually happened:
Pressing Yes results in no message being displayed. The 4-second wait time does occur, and it ends with the termination of the program, but no label changes are visible through the wait. It's as if the program doesn't recognize labels when a sleep() function is in the same conditional branch.

I've tried adding text into output_label below the sleep() function, but there was still no appearance of text in the execution.

0

There are 0 answers