Qt progressbar update value



  • I have a progressbar in my code.
    ui->progressBar->setRange(0, Nbr_TSendConfig);
    ui->progressBar->setValue(0);
    ui->progressBar->show();
    Here is part of my code.
    while(sendleft.length()>0)
    {
    bool flag;
    flag=true;

                taille=sendleft.indexOf(";;;;");
                send=sendleft.left(taille);
                sendleft.remove(0,taille+4);
                if(!data1.sendCardConfig(flag,send,taille))
                {
                    QMessageBox::critical(this, tr("Erreur"), tr("Echec de la configuration!"), QMessageBox::Ok);
                    ui->progressBar->hide();
                    return 0;
                }
                nbr_T++;
                ui->progressBar->setValue(nbr_T);
            }
    

    with this code, sometimes it works fine. But sometimes, il blocks the update of the progress bar, and after a few seconds it jumps to 100% directly at the end.
    Why this happens? Is my code lack of something? How can I fix it?


  • Qt Champions 2016

    Hi
    Your while loop might kill the global event loop and all stops to draw.
    Try calling
    qApp->processEvents();
    in the loop.
    This is just a workaround. If you need longer lasting loops you should
    try to avoid for loops and see if u can design a solution using signals and slots.
    Or use a thread.

    In any case if for LOOP in the GUI thread too much . all will stop to work. :)



  • you are right, now it works fine.


Log in to reply
 

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