Unsolved progressbar level indicator
-
Hi All,
I've progress bar to display the instantaneous value of real-time sensor.
the range for the
QProgressBar
is set asui->progressBar->setRange(0, 0.5*100); // to make range integer ui->progressBar->setValue(sensor_read*100);
It is working ok.
Now I need to set a "threshold" value using adoubleSpinBox
, as shown below:The position of the dotted line (which is a
QLabel
) is the threshold that can be set using adoubleSpinBox
.My requirement is to change the height of the dotted line with respect to the threshold value.
the top and bottom
y
coordinates of theQProgressBar
is250
and450
How do I get proportional
y
coordinate for theQLabel
(dotted line) when I set a threshold value usingdoubleSpinBox
? -
@viniltc
Basic math?250 + (450 - 250) * percentage
?top + height * percentage
? Or pretty similar? -
@viniltc the easiest way is probably to place a Vertical Layout onto of your progress bar and then manipulate the stretch values according to the spin box value
like in this example:
int main (int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; w.resize (200, 400); QHBoxLayout *hLay(new QHBoxLayout(&w)); QProgressBar pBar; pBar.setOrientation(Qt::Vertical); pBar.setMinimumWidth(50); QSpinBox sBox; sBox.setRange(0,100); hLay->addWidget(&pBar); hLay->addWidget(&sBox); QVBoxLayout *vLay(new QVBoxLayout(&pBar)); vLay->addSpacerItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding)); vLay->addWidget(new QLabel ("________")); vLay->addSpacerItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding)); QObject::connect(&sBox, QOverload<int>::of(&QSpinBox::valueChanged), &pBar, [vLay](int value)->void{ vLay->setStretch(0, 100 - value); vLay->setStretch(1,1); vLay->setStretch(2, value); }); sBox.setValue(20); w.show(); return a.exec(); }
-
@J-Hilk and @JonB Thanks a lot for your feedback.
@JonB said in progressbar level indicator:
Basic math?
250 + (450 - 250) * percentage
?top + height * percentage
? Or pretty similar?Could you explain what you mean by
percentage
here?I 'm trying to do something like this (changing the
y
coordinate oflabel
):double value = 450-(ui->doubleSpinBox->value()*100); // where 450 is the bottom of progress bar, here value is not proportional to the progressbar limit ui->label->setGeometry(140,value,81,16);
But I'm a bit stuck to find a range for
value
also to make sure it chnage in proporation to theQProgressbar
. Any suggestions? -
@viniltc
You have some minimum value for the bar, saylo
. You have some maximum value, sayhi
. And a value, sayvalue
. Doesn't matter whether those are integers, floats or whatever. So by "percentage" (actually fraction) I mean:(value - lo) / (hi - lo)
You want your
y
coordinate to beminy + (maxy - miny) * percentage
.