I know I'm late the the game on this particular request, but I recently had the same need and found what I think is a better and easier solution. In particular, I liked the look of the base style and only wanted the color of the chunk, and for light colored chunks, have a dark text over it.
The general principle is to make a copy of the QPalette for the QProgressBar in question. Then change QPalette::Highlight role color and the QPalette::HighlightText role based on the parameter being displayed in the copy and then assign the QPalette to the QProgressBar. Do this in the valueChanged(int) slot. In my example, I was assessing a motor speed.
void MainWindow::on_TachometerBar_valueChanged(int value)
{
static QPalette p(ui->TachometerBar->palette()); //Static used here to only initialize once
if (value < 600)
{
p.setColor(QPalette::Highlight, Qt::yellow);
p.setColor(QPalette::HighlightedText, Qt::black);
}
else if (value < 1800)
{
p.setColor(QPalette::Highlight, Qt::darkGreen);
p.setColor(QPalette::HighlightedText, Qt::white);
}
else if (value < 2000)
{
p.setColor(QPalette::Highlight, Qt::yellow);
p.setColor(QPalette::HighlightedText, Qt::black);
}
else
{
p.setColor(QPalette::Highlight, Qt::red);
p.setColor(QPalette::HighlightedText, Qt::white);
}
ui->TachometerBar->setPalette(p);
}
[image: 2a253b91-3d26-4d73-854c-af80d74579ee.png]
[image: fffdcb68-1aa0-4487-8470-bcc6f6d706ba.png]
[image: 0dc7e0c5-5377-4c7c-bc57-a883b7399bb8.png]
[image: f31e6199-cbbe-4eec-8e87-e55a44b3e19e.png]