0

I'm trying to make a progress bar that can dynamically change part of it's color depending on a slider's value. The closest I can get to that right now is by using:

bar->setStyleSheet(QString("QProgressBar::chunk:vertical {background: qlineargradient(x1:0, y1:0, x2: 0, y2: 1, stop:0 red, stop:0.5 green); border-radius: 9px;}")
                   +QString("QProgressBar::vertical {border: 1px solid #b4adad; border-radius: 9px; background: #2f2828; padding: 0px; text-align: left; margin-right: 4ex;}"));

I have tried setting the second stop point to slider->value() which takes away the whole style sheet leaving me with a blank progress bar. I have tried just using CSS code which also takes away the style sheet:

{background: linear-gradient(to bottom, white 0%, blue 25%, blue 100%); border-radius: 9px;}"

I'm confused because I can use CSS to set the background but I can't get it to work unless I use qlineargradient, why is this? What needs to be done in order to implement CSS in a Qt stylesheet without restriction?

Is it possible to set the value of a stop point to the changing value of a slider?

I also attempted using the setStyleSheet function within an if statement so that the stylesheet itself will change depending on the value of the progress bar:

if (bar->value()<slider->value()) {

however this doesn't dynamically change the stylesheet. It seems as though it runs the if statement one time prior to opening the app. Does QT run a while loop that runs through the code continuously while the app is open or am I mistaken?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
galeotron
  • 47
  • 1
  • 7

1 Answers1

0

From https://doc.qt.io/qt-5/qprogressbar.html#valueChanged, there is a signal void QProgressBar::valueChanged(int value) that you can connect to a slot and make the change you want for the QProgressBar sylesheet, for example :

void MainWindow::on_progressBar_valueChanged(int value)
{
    if (value >= 0 && value < 50)
        ui->progressBar->setStyleSheet("border: 2px solid grey; border-radius: 5px; text-align: center;");
    else if (value >= 50 && value < 75)
        ui->progressBar->setStyleSheet("background-color: #05B8CC; width: 20px;");
    // and so on ...
}
Fryz
  • 2,119
  • 2
  • 25
  • 45
  • This is wrong. "background-color" does not change the colour of the bar. `'QProgressBar::chunk{background-color: lightgrey;}'` is the sort of thing to try... but this can change other style elements unintentionally. – mike rodent Jun 06 '23 at 16:56