# Thread: QProgressBar with % in decimal

1. Novice
Join Date
Jan 2020
Posts
22
Qt products
Platforms

## QProgressBar with % in decimal

What is the best/easiest way to modify QProgressBar to show the %-value as a decimal value? I presume somebody will have done this before, but I have not been able to find it.

2. Advanced user
Join Date
Jan 2006
Location
Bremen, Germany
Posts
491
Thanked 73 Times in 69 Posts
Qt products
Platforms

## Re: QProgressBar with % in decimal

3. Guru
Join Date
Mar 2009
Location
Brisbane, Australia
Posts
7,618
Thanks
13
Thanked 1,591 Times in 1,520 Posts
Qt products
Platforms
Wiki edits
17

## Re: QProgressBar with % in decimal

Not sure I see how setFormat() can be used to get a percentage value that is not integer. The placeholder "%p" gets an integer as a string. You could get a string like "9 of 37" from "%v of %m" but you cannot compute with these placeholders.

You could subclass QProgressBar and override QProgressBar::text():
Qt Code:
1. #ifndef FRACTIONALPROGRESSBAR_H
2. #define FRACTIONALPROGRESSBAR_H
3.
4. #include <QProgressBar>
5.
6. class FractionalProgressBar : public QProgressBar
7. {
8. public:
9. FractionalProgressBar(QWidget *p = nullptr);
10.
11. // QProgressBar interface
12. public:
13. virtual QString text() const Q_DECL_OVERRIDE;
14. };
15.
16. #endif // FRACTIONALPROGRESSBAR_H
To copy to clipboard, switch view to plain text mode
Qt Code:
1. #include "fractionalprogressbar.h"
2.
3. FractionalProgressBar::FractionalProgressBar(QWidget *p):
4. {
5. }
6.
7.
8. QString FractionalProgressBar::text() const
9. {
10. if ( minimum() == maximum() ) // divide by zero guard
11. return QString();
12.
13. double percent = 100.0 * (value() - minimum()) / (maximum() - minimum());
14. return QString("%1%").arg(percent, 0, 'f', 1);
15. }
To copy to clipboard, switch view to plain text mode

4. Novice
Join Date
Jan 2020
Posts
22
Qt products
Platforms

## Re: QProgressBar with % in decimal

But the setValue() method converts the input argument to int, so the fractional part of the value is lost.

5. ## Re: QProgressBar with % in decimal

But the setValue() method converts the input argument to int, so the fractional part of the value is lost.
Change the scale of the progress bar range. If you now have it set to range from 0 - 100, and you want one decimal place, change it to a range of 0 - 1000. Use ChrisW67's method to format the text by converting the value to a float, dividing by 10, and use the result to set a float formatted string.

You will also have to change the code that sets the value to multiply the number passed to setValue() by 10.

6. Novice
Join Date
Jan 2020
Posts
22
Qt products
Platforms

## Re: QProgressBar with % in decimal

Works perfectly! Thanks. I tried to find a good excuse for not thinking of this myself, but couldn't find one ...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.