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
Forum Updated to NodeBB v4.3 + New Features

QProgressBar breaks with large values

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.3k 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.
  • K Offline
    K Offline
    Kekz
    wrote on 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      6
      • Christian EhrlicherC Christian Ehrlicher

        Then use scaled values for your progress bar.

        K Offline
        K Offline
        Kekz
        wrote on 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?

        Pablo J. RoginaP 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          1
          • Christian EhrlicherC Christian Ehrlicher

            @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 last edited by Kekz
            #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...

            JonBJ 1 Reply Last reply
            0
            • K Kekz

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

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on 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

                @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...

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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
                4
                • JonBJ JonB

                  @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 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.

                  JonBJ 1 Reply Last reply
                  0
                  • nageshN Offline
                    nageshN Offline
                    nagesh
                    wrote on 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

                      @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.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      3
                      • JonBJ JonB

                        @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 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.

                        JonBJ 1 Reply Last reply
                        2
                        • K Kekz

                          @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.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #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

                          • Login

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