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 breaks with large values
QtWS25 Last Chance

QProgressBar breaks with large values

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.2k Views
  • 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.
  • K Offline
    K Offline
    Kekz
    wrote on 8 Mar 2021, 11:07 last edited by
    #1

    I am using Qt 5.15.0
    I have a program that takes a file and hashes its content and I want to display the progress with a ProgressBar. Everything works fine for mid-sized files.
    The problem comes if the file is > 2GB in size. Since QProgressBar::setRange() only takes signed 32-bit integers, if my file is 3GB large, the upper limit
    will overflow and become negative, causing the ProgressBar to reset and show no progress at all.
    Similar thing if the file is 5GB large: The upper limit overflows and gets set to 1GB, this time showing progress but incorrectly since the progress bar will be full when we have only hashed 1 out of 5 GBs.
    How could this be solved? I do the percentage calculation on my own using 64-bit values, so the percentage text displayed is correct, however the progress bar fills up too quickly or doesn't move at all, depending on how the value overflows.
    I could track the progress in KBs or use a value range from 0-100 but that would cost a lot of precision and doesn't really look like a good solution to me.
    I am not that familiar with Qt, this is my first project using it, so any tips are appreciated.
    Thanks for reading.

    In case it is needed, this is the code that updates the progress bar (signal emitted from another thread):

    void Controller::UpdateFileProgress(const size_t min, const size_t max, const size_t value)
    {
        ui->fileProgressBar->setRange((int)min, (int)max);
        ui->fileProgressBar->setValue((int)std::min(value, max));
    
        if (value >= max)
        {
            ui->fileProgressBar->setFormat("100%");
        }
        else
        {
            double percentage = static_cast<double>(value) / static_cast<double>(max);
            ui->fileProgressBar->setFormat(QString::number((percentage * 100), 'g', 3)+ "%");
        }
    }
    
    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Mar 2021, 11:14 last edited by
      #2

      Then use scaled values for your progress bar.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      K 1 Reply Last reply 8 Mar 2021, 11:18
      6
      • C Christian Ehrlicher
        8 Mar 2021, 11:14

        Then use scaled values for your progress bar.

        K Offline
        K Offline
        Kekz
        wrote on 8 Mar 2021, 11:18 last edited by
        #3

        @Christian-Ehrlicher That was also one of my thoughts but I was hoping there would be a better way.
        Is it possible to extends the QProgressBar class and override these methods and attributes?

        P 1 Reply Last reply 8 Mar 2021, 12:36
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 8 Mar 2021, 11:20 last edited by
          #4

          @Kekz said in QProgressBar breaks with large values:

          Is it possible to extends the QProgressBar class and override these methods and attributes?

          No and I don't see why it should be needed. If you need it derive from QProgressBar and do the scaling there but adding a /100 or similar before passing the values isn't worth the trouble imo.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          K 1 Reply Last reply 8 Mar 2021, 11:28
          1
          • C Christian Ehrlicher
            8 Mar 2021, 11:20

            @Kekz said in QProgressBar breaks with large values:

            Is it possible to extends the QProgressBar class and override these methods and attributes?

            No and I don't see why it should be needed. If you need it derive from QProgressBar and do the scaling there but adding a /100 or similar before passing the values isn't worth the trouble imo.

            K Offline
            K Offline
            Kekz
            wrote on 8 Mar 2021, 11:28 last edited by Kekz 3 Aug 2021, 11:35
            #5

            @Christian-Ehrlicher Deriving was what I meant, sry. Then I will probably go that route and use 64-bit values, thanks for the help.
            On second thought, that probably won't work. I will need to resort to scaling...

            J 1 Reply Last reply 8 Mar 2021, 13:02
            0
            • K Kekz
              8 Mar 2021, 11:18

              @Christian-Ehrlicher That was also one of my thoughts but I was hoping there would be a better way.
              Is it possible to extends the QProgressBar class and override these methods and attributes?

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 8 Mar 2021, 12:36 last edited by
              #6

              @Kekz said in QProgressBar breaks with large values:

              Is it possible to extends the QProgressBar class and override these methods and attributes?

              Yes, it is.
              The question could be: is it worth? rather than scale the values as @Christian-Ehrlicher suggested...

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • K Kekz
                8 Mar 2021, 11:28

                @Christian-Ehrlicher Deriving was what I meant, sry. Then I will probably go that route and use 64-bit values, thanks for the help.
                On second thought, that probably won't work. I will need to resort to scaling...

                J Offline
                J Offline
                JonB
                wrote on 8 Mar 2021, 13:02 last edited by JonB 3 Aug 2021, 13:04
                #7

                @Kekz
                If it floats your boat, derive from QProgressBar and write your own setRange(), setValue() etc. new method overloads which accept a size_t or double or whatever. And that can do the division to do the "scaling" there, so your callers won't have to.

                K 1 Reply Last reply 9 Mar 2021, 22:48
                4
                • J JonB
                  8 Mar 2021, 13:02

                  @Kekz
                  If it floats your boat, derive from QProgressBar and write your own setRange(), setValue() etc. new method overloads which accept a size_t or double or whatever. And that can do the division to do the "scaling" there, so your callers won't have to.

                  K Offline
                  K Offline
                  Kekz
                  wrote on 9 Mar 2021, 22:48 last edited by
                  #8

                  @JonB
                  I was hoping to be able to also override the value, min and max attributes to force Qt to use 64-bit values, but that doesn't seem to work.
                  I'll have to scale the values down, it's an ugly solution but the only one that seems possible.

                  J 1 Reply Last reply 10 Mar 2021, 09:56
                  0
                  • N Offline
                    N Offline
                    nagesh
                    wrote on 9 Mar 2021, 23:53 last edited by
                    #9

                    @Kekz Internally

                    ProgressBar will display the percentage of steps that have been 
                    completed when you later give it the current step value. 
                    
                    The percentage is calculated by dividing the progress
                     (value() - minimum()) / (maximum() - minimum())
                    

                    So you can set value min max as 0,SCALE_MAX always irrespective file size.
                    ui->fileProgressBar->setRange(0,SCALE_MAX );

                    void Controller::UpdateFileProgress( const size_t FileSize, const size_t value)
                    

                    double ratio= static_cast<double>(value) / static_cast<double>(FileSize);
                    int scaledvalue =( ratio * SCALE_MAX );

                    1 Reply Last reply
                    1
                    • K Kekz
                      9 Mar 2021, 22:48

                      @JonB
                      I was hoping to be able to also override the value, min and max attributes to force Qt to use 64-bit values, but that doesn't seem to work.
                      I'll have to scale the values down, it's an ugly solution but the only one that seems possible.

                      J Offline
                      J Offline
                      JonB
                      wrote on 10 Mar 2021, 09:56 last edited by
                      #10

                      @Kekz said in QProgressBar breaks with large values:

                      I was hoping to be able to also override the value, min and max attributes to force Qt to use 64-bit values, but that doesn't seem to work.

                      You cannot "override" variables, or the type of variables, in C++ (e.g. you cannot declare variables virtual). QProgressBar uses ints (32-bit) for these, and you cannot alter that.

                      I'll have to scale the values down, it's an ugly solution but the only one that seems possible.

                      I do not see this as even vaguely "ugly". QProgressBar works off a range of possible values something like -2,000,000,000 to +2,000,000,000, which is more than enough to cover the visible "steps" available on the bar, and that's what you must supply it with for min/max/value. Scaling the number from, say a float to an int seems fine to me, and you can add your own methods to your QProgressBar subclass to make this invisible to the outside world.

                      K 1 Reply Last reply 10 Mar 2021, 17:17
                      3
                      • J JonB
                        10 Mar 2021, 09:56

                        @Kekz said in QProgressBar breaks with large values:

                        I was hoping to be able to also override the value, min and max attributes to force Qt to use 64-bit values, but that doesn't seem to work.

                        You cannot "override" variables, or the type of variables, in C++ (e.g. you cannot declare variables virtual). QProgressBar uses ints (32-bit) for these, and you cannot alter that.

                        I'll have to scale the values down, it's an ugly solution but the only one that seems possible.

                        I do not see this as even vaguely "ugly". QProgressBar works off a range of possible values something like -2,000,000,000 to +2,000,000,000, which is more than enough to cover the visible "steps" available on the bar, and that's what you must supply it with for min/max/value. Scaling the number from, say a float to an int seems fine to me, and you can add your own methods to your QProgressBar subclass to make this invisible to the outside world.

                        K Offline
                        K Offline
                        Kekz
                        wrote on 10 Mar 2021, 17:17 last edited by
                        #11

                        @JonB I guess you are right, my concerns about loosing precision were unfounded since 2 billion steps will always be more than enough.
                        I will do as @nagesh (and many others) suggested and mark this as solved.

                        J 1 Reply Last reply 10 Mar 2021, 18:23
                        2
                        • K Kekz
                          10 Mar 2021, 17:17

                          @JonB I guess you are right, my concerns about loosing precision were unfounded since 2 billion steps will always be more than enough.
                          I will do as @nagesh (and many others) suggested and mark this as solved.

                          J Offline
                          J Offline
                          JonB
                          wrote on 10 Mar 2021, 18:23 last edited by JonB 3 Oct 2021, 18:23
                          #12

                          @Kekz
                          A 32-bit has 4 billion range. Your horizontal screen resolution is, say, 1,920 pixels. So 2 million bits available per pixel the user can see. I don't think losing precision will be an issue :)

                          1 Reply Last reply
                          0

                          3/12

                          8 Mar 2021, 11:18

                          9 unread
                          • Login

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