Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
    
    ui->progressBar->setValue(sensor_read*100);
    

    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;
        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 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
     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 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