QTimer with QProgressbar



  • Hi,

    I am trying to make my progressbar flow by time so I connected a progressbar to a QTimer. Here is my code:

    void MainWindow::progressbarTimer(QString TimeSet)
    {
        ui->progressBar->setMaximum(TimeSet.toInt()*1000);
        QTimer *timer = new QTimer(this);
        currenttime = TimeSet.toInt();
        connect(timer, SIGNAL(timeout()), this, SLOT(updateProgresbar()));
        timer->start(1);
    }
    
    void MainWindow::updateProgresbar()
    {
        if(counter <= currenttime*1000)
        {
            counter++;
            ui->progressBar->setValue(counter);
        }
        else
        {
            timer->stop();
            delete timer;
        }
    }
    

    Unfortunately I can't seem to input parameters into the updateProgresbar slot to make use of the timer in the function. Anyone got a solution for this? Thanks in advance!


  • Lifetime Qt Champion

    Hi,

    You declare timer in progressbarTimer so it's not the same as the one you have in updateProgresbar.



  • @SGaist Hi, thanks for your reply. I know it is not the same. My question is how I can get it the same.



  • Hio,
    What SGaist said is that your timer (QTimer) in void MainWindow::progressbarTimer() has function scope. So when you end the function, the timer variable is no longer available. Because you don't delete it and it has a signal/slot connection the timer is still active in the eventloop. So calling the updateProgressBar is no problem. To reference again to that timer is not valid!! the timer-> will give errors??


Log in to reply
 

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