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. QProgressBar doesn't update
Qt 6.11 is out! See what's new in the release blog

QProgressBar doesn't update

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 2.3k Views 3 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm trying to make a progress bar for a file download, but I can't get it to work.
    I'm sending information from a thread to mainwindow, where the progress bar should update, but it doesn't do anything
    Here's what I have:

    QProgressBar* progBar = new QProgressBar;
    if (percentage < 0) {
        ui->gridLayout_Content->addWidget(progBar);
    } else if (percentage > 0) {
        qDebug() << percentage; //this works, but progBar doesn't update
        progBar->setValue(percentage);
        progBar->update();
    } else if (percentage > 100) {
        delete progBar;
    }
    
    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      Hi, I'm trying to make a progress bar for a file download, but I can't get it to work.
      I'm sending information from a thread to mainwindow, where the progress bar should update, but it doesn't do anything
      Here's what I have:

      QProgressBar* progBar = new QProgressBar;
      if (percentage < 0) {
          ui->gridLayout_Content->addWidget(progBar);
      } else if (percentage > 0) {
          qDebug() << percentage; //this works, but progBar doesn't update
          progBar->setValue(percentage);
          progBar->update();
      } else if (percentage > 100) {
          delete progBar;
      }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Sucharek
      In principle the approach should work.

      • Where is this code?
      • If it is really as shown, you would create a new progress bar each time this code is called to show a new value. Also is it ever called with percentage < 0? Maybe one of these is your issue, I don't know.
      • Make sure you really are sending a signal from the thread and the code above is in the main UI thread, in a slot.
      • If your percentage is 0--100, verify what the progress bar's range is.
      S 1 Reply Last reply
      3
      • JonBJ JonB

        @Sucharek
        In principle the approach should work.

        • Where is this code?
        • If it is really as shown, you would create a new progress bar each time this code is called to show a new value. Also is it ever called with percentage < 0? Maybe one of these is your issue, I don't know.
        • Make sure you really are sending a signal from the thread and the code above is in the main UI thread, in a slot.
        • If your percentage is 0--100, verify what the progress bar's range is.
        S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #3

        Hi @JonB, the code is as a private slot (void) in MainWindow.
        I'm calling percentage < 0 before the download starts.
        I didn't set the range, so I did that, but nothing changed.
        I know that the percentage values are being sent, because when I run qDebug(), it prints the percentages just fine.

        JonBJ 1 Reply Last reply
        0
        • S Sucharek

          Hi @JonB, the code is as a private slot (void) in MainWindow.
          I'm calling percentage < 0 before the download starts.
          I didn't set the range, so I did that, but nothing changed.
          I know that the percentage values are being sent, because when I run qDebug(), it prints the percentages just fine.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Sucharek
          Show your actual code, like I said it can't be right to have what you presently show.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @Sucharek
            Show your actual code, like I said it can't be right to have what you presently show.

            S Offline
            S Offline
            Sucharek
            wrote on last edited by
            #5

            Ok
            Here is the full void in MainWindow.

            void MainWindow::progressBar(int percentage)
            {
                QProgressBar* progBar = new QProgressBar;
                progBar->setRange(0, 100);
                if (percentage < 0) {
                    ui->gridLayout_Content->addWidget(progBar);
                } else if (percentage > 0) {
                    qDebug() << percentage;
                    progBar->setValue(percentage);
                    progBar->update();
                } else if (percentage > 100) {
                    delete progBar;
                }
            }
            

            Here is the connector:

            QObject::connect(&fT,SIGNAL(progBar(const int&)),SLOT(progressBar(const int&)), Qt::QueuedConnection);
            

            Code in thread:

            //start download
            progBar(-1);
            while(true) {
                QString percentage = getPercentage(dir + "ROM/");
                dlProg = "Downloading ROM... (" + percentage + "%)"; update(checkInternet + dlProg); //I'm also updating a label, where the percentage was shown originally
                progBar(percentage.toInt()); //here I'm emiting the percentage values
            
                avoidComplete += 1;
                if (avoidComplete > 40) {if (percentage == "100") {progBar(101); /*this is hide the bar after finished*/ break;}}
            
                msleep(100);
            }
            
            JonBJ jsulmJ J.HilkJ 3 Replies Last reply
            0
            • S Sucharek

              Ok
              Here is the full void in MainWindow.

              void MainWindow::progressBar(int percentage)
              {
                  QProgressBar* progBar = new QProgressBar;
                  progBar->setRange(0, 100);
                  if (percentage < 0) {
                      ui->gridLayout_Content->addWidget(progBar);
                  } else if (percentage > 0) {
                      qDebug() << percentage;
                      progBar->setValue(percentage);
                      progBar->update();
                  } else if (percentage > 100) {
                      delete progBar;
                  }
              }
              

              Here is the connector:

              QObject::connect(&fT,SIGNAL(progBar(const int&)),SLOT(progressBar(const int&)), Qt::QueuedConnection);
              

              Code in thread:

              //start download
              progBar(-1);
              while(true) {
                  QString percentage = getPercentage(dir + "ROM/");
                  dlProg = "Downloading ROM... (" + percentage + "%)"; update(checkInternet + dlProg); //I'm also updating a label, where the percentage was shown originally
                  progBar(percentage.toInt()); //here I'm emiting the percentage values
              
                  avoidComplete += 1;
                  if (avoidComplete > 40) {if (percentage == "100") {progBar(101); /*this is hide the bar after finished*/ break;}}
              
                  msleep(100);
              }
              
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @Sucharek
              In that case please re-read my first answer. I gave you 4 possibilities. You answered for 3 of them sataisfactorily. The one you ignored is what is wrong in your code.

              On a separate stylistic matter:

              • Use emit as a matter of course when emitting signals.
              • Don't specify Qt::QueuedConnection explicitly, let Qt figure that for you when the signal & slot are not in the same thread.
              S 1 Reply Last reply
              1
              • S Sucharek

                Ok
                Here is the full void in MainWindow.

                void MainWindow::progressBar(int percentage)
                {
                    QProgressBar* progBar = new QProgressBar;
                    progBar->setRange(0, 100);
                    if (percentage < 0) {
                        ui->gridLayout_Content->addWidget(progBar);
                    } else if (percentage > 0) {
                        qDebug() << percentage;
                        progBar->setValue(percentage);
                        progBar->update();
                    } else if (percentage > 100) {
                        delete progBar;
                    }
                }
                

                Here is the connector:

                QObject::connect(&fT,SIGNAL(progBar(const int&)),SLOT(progressBar(const int&)), Qt::QueuedConnection);
                

                Code in thread:

                //start download
                progBar(-1);
                while(true) {
                    QString percentage = getPercentage(dir + "ROM/");
                    dlProg = "Downloading ROM... (" + percentage + "%)"; update(checkInternet + dlProg); //I'm also updating a label, where the percentage was shown originally
                    progBar(percentage.toInt()); //here I'm emiting the percentage values
                
                    avoidComplete += 1;
                    if (avoidComplete > 40) {if (percentage == "100") {progBar(101); /*this is hide the bar after finished*/ break;}}
                
                    msleep(100);
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7
                This post is deleted!
                JonBJ 1 Reply Last reply
                0
                • jsulmJ jsulm

                  This post is deleted!

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  1
                  • S Sucharek

                    Ok
                    Here is the full void in MainWindow.

                    void MainWindow::progressBar(int percentage)
                    {
                        QProgressBar* progBar = new QProgressBar;
                        progBar->setRange(0, 100);
                        if (percentage < 0) {
                            ui->gridLayout_Content->addWidget(progBar);
                        } else if (percentage > 0) {
                            qDebug() << percentage;
                            progBar->setValue(percentage);
                            progBar->update();
                        } else if (percentage > 100) {
                            delete progBar;
                        }
                    }
                    

                    Here is the connector:

                    QObject::connect(&fT,SIGNAL(progBar(const int&)),SLOT(progressBar(const int&)), Qt::QueuedConnection);
                    

                    Code in thread:

                    //start download
                    progBar(-1);
                    while(true) {
                        QString percentage = getPercentage(dir + "ROM/");
                        dlProg = "Downloading ROM... (" + percentage + "%)"; update(checkInternet + dlProg); //I'm also updating a label, where the percentage was shown originally
                        progBar(percentage.toInt()); //here I'm emiting the percentage values
                    
                        avoidComplete += 1;
                        if (avoidComplete > 40) {if (percentage == "100") {progBar(101); /*this is hide the bar after finished*/ break;}}
                    
                        msleep(100);
                    }
                    
                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @Sucharek besides all the other issues you have with your code,

                    your QPorgressbar does neither have a QWidget parent, that is visible, nor do you call show on it

                    -> you never see any of the countless QProgressbar widgets you create.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Sucharek
                      In that case please re-read my first answer. I gave you 4 possibilities. You answered for 3 of them sataisfactorily. The one you ignored is what is wrong in your code.

                      On a separate stylistic matter:

                      • Use emit as a matter of course when emitting signals.
                      • Don't specify Qt::QueuedConnection explicitly, let Qt figure that for you when the signal & slot are not in the same thread.
                      S Offline
                      S Offline
                      Sucharek
                      wrote on last edited by
                      #10

                      @JonB, I used emit, nothing changed.
                      I replace Qt::QueuedConnection to Qt::AutoConnection, but that also didn't do anything.

                      JonBJ 1 Reply Last reply
                      0
                      • S Sucharek

                        @JonB, I used emit, nothing changed.
                        I replace Qt::QueuedConnection to Qt::AutoConnection, but that also didn't do anything.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Sucharek
                        I said those two were stylistic.

                        I already typed in what is wrong in your code. I told you there were 4 bullet points. I told you you had answered 3 of them satisfactorily. I told you that the one you ignored is what is wrong with your code. Also in @J-Hilk's post he mentions the same issue at one point. I don't feel like re-typing it when it's already there. If you still need help on it come back, but at least take the time to read which one you ignored first.....

                        S 1 Reply Last reply
                        1
                        • SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by SPlatten
                          #12

                          @Sucharek , looking at your source, you create a new progress bar in a function:

                          void MainWindow::progressBar(int percentage)
                          {
                              QProgressBar* progBar = new QProgressBar;
                          

                          Some place elsewhere you then connect a signal and slot:

                          QObject::connect(&fT,SIGNAL(progBar(const int&)),SLOT(progressBar(const int&)), Qt::QueuedConnection);
                          

                          What is fT, is this actually in the same place as you create the progress bar?

                          What version of Qt are you using? progBar isn't a progress bar signal.

                          Kind Regards,
                          Sy

                          S 1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @Sucharek , looking at your source, you create a new progress bar in a function:

                            void MainWindow::progressBar(int percentage)
                            {
                                QProgressBar* progBar = new QProgressBar;
                            

                            Some place elsewhere you then connect a signal and slot:

                            QObject::connect(&fT,SIGNAL(progBar(const int&)),SLOT(progressBar(const int&)), Qt::QueuedConnection);
                            

                            What is fT, is this actually in the same place as you create the progress bar?

                            What version of Qt are you using? progBar isn't a progress bar signal.

                            S Offline
                            S Offline
                            Sucharek
                            wrote on last edited by
                            #13

                            Hi @SPlatten, fT is the thread name.
                            The QObject::connect calls the MainWindow::progBar slot.

                            I'm using Qt 5.15.2 (static).

                            SPlattenS 1 Reply Last reply
                            0
                            • S Sucharek

                              Hi @SPlatten, fT is the thread name.
                              The QObject::connect calls the MainWindow::progBar slot.

                              I'm using Qt 5.15.2 (static).

                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by SPlatten
                              #14

                              @Sucharek , I suggest you get it working with simple code first. Also isn't your connect missing the pointer to the progress bar as the third parameter ?

                              Kind Regards,
                              Sy

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Sucharek
                                I said those two were stylistic.

                                I already typed in what is wrong in your code. I told you there were 4 bullet points. I told you you had answered 3 of them satisfactorily. I told you that the one you ignored is what is wrong with your code. Also in @J-Hilk's post he mentions the same issue at one point. I don't feel like re-typing it when it's already there. If you still need help on it come back, but at least take the time to read which one you ignored first.....

                                S Offline
                                S Offline
                                Sucharek
                                wrote on last edited by
                                #15

                                @JonB, ok, I think I understood the 4th solution.
                                I created a new progress bar everytime I called the progBar signal in the thread.
                                It's basically like this: create -> delete -> create -> delete -> ...
                                Here's my code:

                                delete progBar;
                                progBar = new QProgressBar; //progBar is now a global variable in MainWindow
                                progBar->setRange(0, 100);
                                if (percentage < 0) {
                                    ui->gridLayout_Content->addWidget(progBar);
                                } else if (percentage > 0) {
                                    qDebug() << percentage;
                                    ui->gridLayout_Content->addWidget(progBar);
                                    progBar->setValue(percentage);
                                    progBar->update();
                                } else if (percentage > 100) {
                                    delete progBar;
                                }
                                

                                I don't know if that's what you meant, but the most important thing is, that it worked. Thanks

                                JonBJ 1 Reply Last reply
                                0
                                • S Sucharek

                                  @JonB, ok, I think I understood the 4th solution.
                                  I created a new progress bar everytime I called the progBar signal in the thread.
                                  It's basically like this: create -> delete -> create -> delete -> ...
                                  Here's my code:

                                  delete progBar;
                                  progBar = new QProgressBar; //progBar is now a global variable in MainWindow
                                  progBar->setRange(0, 100);
                                  if (percentage < 0) {
                                      ui->gridLayout_Content->addWidget(progBar);
                                  } else if (percentage > 0) {
                                      qDebug() << percentage;
                                      ui->gridLayout_Content->addWidget(progBar);
                                      progBar->setValue(percentage);
                                      progBar->update();
                                  } else if (percentage > 100) {
                                      delete progBar;
                                  }
                                  

                                  I don't know if that's what you meant, but the most important thing is, that it worked. Thanks

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @Sucharek said in QProgressBar doesn't update:

                                  I created a new progress bar everytime I called the progBar signal in the thread.

                                  :) Well done, it's important to see things for yourself!

                                  However, it still looks like you have progBar = new QProgressBar; in your slot?? The first 3 lines need to be somewhere else, either outside the slot or maybe in the if (percentage < 0) case.

                                  Oh, and hang on! You have moved [copied] ui->gridLayout_Content->addWidget(progBar); into the case for every percentage update! That's not right [fortunately it won't have any effect]. That too belongs e.g. in the if (percentage < 0), or where you create the progress bar.

                                  Also, be careful about you have two delete progBar;s. And you do not set the member variable to nullptr either time. This is waiting to crash on you with a "duplicate delete".....

                                  S 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @Sucharek said in QProgressBar doesn't update:

                                    I created a new progress bar everytime I called the progBar signal in the thread.

                                    :) Well done, it's important to see things for yourself!

                                    However, it still looks like you have progBar = new QProgressBar; in your slot?? The first 3 lines need to be somewhere else, either outside the slot or maybe in the if (percentage < 0) case.

                                    Oh, and hang on! You have moved [copied] ui->gridLayout_Content->addWidget(progBar); into the case for every percentage update! That's not right [fortunately it won't have any effect]. That too belongs e.g. in the if (percentage < 0), or where you create the progress bar.

                                    Also, be careful about you have two delete progBar;s. And you do not set the member variable to nullptr either time. This is waiting to crash on you with a "duplicate delete".....

                                    S Offline
                                    S Offline
                                    Sucharek
                                    wrote on last edited by
                                    #17

                                    I moved these lines in to initialization void (the one with ui->setup(this)).
                                    I know aboult the duplicate deletes. Thanks for reminding me.

                                    1 Reply Last reply
                                    1

                                    • Login

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