user12800116
user12800116

Reputation:

add negative values to the progress bar

I have a realtime value from a sensor:

 ui->progressBar_retraction->setValue(aX*100); //aX is realtime value that can take positive or negative values over time

In the above expression, the progress bar shown the instantaneous positive values of aX.

How do I show negative values in a different progress bar ?

Upvotes: 0

Views: 636

Answers (1)

Erel
Erel

Reputation: 739

◘ Well, if you want to use another QProgressBar, you obviously need another one. Then simply check the sign of aX and update one of the two accordingly :

if(aX < 0){

     progress_bar_neg->setValue(qAbs(aX)*100);
     progress_bar_pos->setValue(0);

}else{

     progress_bar_pos->setValue(aX*100);
     progress_bar_neg->setValue(0);

}

Note that if you want one of your QProgressbar to fill from right to left, you can use void setInvertedAppearance(bool invert) to set its invertedAppearance to true.

Another alternative using the same QProgressBar could look like :

 ui->progressBar_retraction->setValue(qAbs(aX)*100);
 if(aX < 0){/*Set to a colour*/}
 else{/*Set to another colour*}

To edit the colour :

QPalette p;
p.setColor(QPalette::Highlight, Qt::red); //Change background colour, here red
ui->progressBar_retraction->setPalette(p);
p.setColor(QPalette::HighlightedText , Qt::black); //Change text colour, here black
ui->progressBar_retraction->setPalette(p);

If you want a bi-directional QProgessBar, it is unfortunately not possible by only calling a method as far as I know (correct me if I am wrong).

What I did was an override of void QProgressBar::paintEvent(QPaintEvent *) to start the drawing of the rectangle from the middle. You then simply fix the range of the QProgressBar with void QProgressBar::setRange(int minimum, int maximum). You may even update the colour to make it a bit simpler to see.

Upvotes: 1

Related Questions