Search code examples
c++qtchunksqprogressbar

Changing QProgressbar chunk's color according to a scale


I am trying to change color of a Qprogressbar according to a variable named accuracy which hold values between 0 and 100. So basically if the accuracy is 100 the color should be green and if it is 0 it should become red and I also want it to support the values and colors between. I have write down the following code but it doesn't affect my progress bar's chunk color. Which part of my code is wrong? I appreciate any tips and helps :)

int G = static_cast<int> (2.5 * accuracy);
int R = 255 - G;
for (G=0, R=0; G<255 && R<255; G++, R++)
{
    ui->progressBar->setStyleSheet("QProgressBar::chunk {background-color: rgb(R, G, 0);}");
}

Solution

  • "QProgressBar::chunk {background-color: rgb(R, G, 0);}" : R and G in this string do not access the variables R and G;

    Q&D and untested:

    QString("QProgressBar::chunk {background-color: rgb(") + QString::number(R) + ", " + QString::number(G) + ", 0);}"