Reputation:
I have to show the instantaneous value of a sensor output using a QProgressbar
.
value can change from 0
to 0.3
how do I configure (set range and set value) the QProgressBar
to display the above value.
I'm a bit confused because setRange
method can only take int
values, and how do I set range as 0
and 0.3
?
Upvotes: 1
Views: 886
Reputation: 1
If your progress bar has range 0 to 4500.0, first set the range.
ui->progressBar->setRange(0.0, 4500.0);
And then, use the followings during progressing:
double percent = 100.0f*currentValue/maximumValue.toFloat();
QString format = tr("%1 %").arg(QString::number(percent, 'f', 1)); // 1 decimal point
ui->progressBar->setFormat(format);
ui->progressBar->setValue(currentValue);
Upvotes: 0
Reputation: 4049
There are different way. You can chose between these two options:
Transform your range in an integer range by multiplying by 100
[0 , 0.3]
->[0 , 300]
When you receive a new value, just multiply it by 1000.12*100
=120/300
You can also make setRange(0, 100)
and for each value, make the conversion:
(value * maxValue) / 100
So(0.12 / 0.3) * 100
give you 40%
Upvotes: 1