Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTimer with QProgressbar
Qt 6.11 is out! See what's new in the release blog

QTimer with QProgressbar

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ? 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          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??

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved