Important: Please read the Qt Code of Conduct -

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 as

    ui->progressBar->setRange(0, 0.5*100); // to make range integer

    It is working ok.
    Now I need to set a "threshold" value using a doubleSpinBox , as shown below:

    enter image description here

    The position of the dotted line (which is a QLabel) is the threshold that can be set using a doubleSpinBox.

    My requirement is to change the height of the dotted line with respect to the threshold value.

    the top and bottom y coordinates of the QProgressBar is 250 and 450

    How do I get proportional y coordinate for the QLabel (dotted line) when I set a threshold value using doubleSpinBox?

  • @viniltc
    Basic math? 250 + (450 - 250) * percentage? top + height * percentage? Or pretty similar?

  • Moderators

    @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;
        QSpinBox 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(2, value);
        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 of label):

     double value = 450-(ui->doubleSpinBox->value()*100); // where 450 is the bottom of progress bar, here value is not proportional to the progressbar limit

    But I'm a bit stuck to find a range for value also to make sure it chnage in proporation to the QProgressbar. Any suggestions?

  • @viniltc
    You have some minimum value for the bar, say lo. You have some maximum value, say hi. And a value, say value. 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 be miny + (maxy - miny) * percentage.

Log in to reply