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.
Printable View
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.
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():
Code:
#ifndef FRACTIONALPROGRESSBAR_H #define FRACTIONALPROGRESSBAR_H #include <QProgressBar> { public: // QProgressBar interface public: }; #endif // FRACTIONALPROGRESSBAR_H
Code:
#include "fractionalprogressbar.h" { } { if ( minimum() == maximum() ) // divide by zero guard double percent = 100.0 * (value() - minimum()) / (maximum() - minimum()); }
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.Quote:
But the setValue() method converts the input argument to int, so the fractional part of the value is lost.
You will also have to change the code that sets the value to multiply the number passed to setValue() by 10.
Works perfectly! Thanks. I tried to find a good excuse for not thinking of this myself, but couldn't find one ...