Update QLabel text in a loop



  • Hi,

    I am trying to show the result of each step of my algorithm in a QLabel to verify the execution, but the QLabel doesn't change.
    I think the problem is because each step take 5 second approximately but I don't understand the problem.

    Here is the part of my code :

    for(int t(0) ; t < limit ; t++)
    {
            /*
               execution of the step t of the algorithm ( 5 second approximately )
           */
            resultLabel->setText("result"+ <result here>)
    }
    

    I also tried to do it with a QMessageBox::information and it works, but I have to click Ok every time :

    for(int t(0) ; t < limit ; t++)
    {
            /*
               execution of the step t of the algorithm ( 5 second approximately )
           */
           QMessageBox::information(this,"result",<result here>)
    }
    
    

    Thanks



  • Hi, you can force a refresh of your QLabel by calling processEvents():

    for(int t(0) ; t < limit ; t++)
    {
            /*
               execution of the step t of the algorithm ( 5 second approximately )
           */
            resultLabel->setText("result"+ <result here>);
    
            qApp->processEvents();
    `}
    


  • Thanks for your answer, unfortunately this part of my code is executed in a class that inherits from QMainWindow, I don't have access to the QApplication.

    Edit:

    It works using QCoreApplication::processEvents() instead.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.