I don't know how to change the color of the text partially in the progress bar when its value becomes nearly 50%. This effect comes automatically in the fusion style progress bar (picture below). Does anyone know how this is done ?
Asked
Active
Viewed 7,209 times
10
-
What problems do you have with implementation, that described in provided link? – Dmitry Sazonov Aug 01 '13 at 09:05
-
1So you propose someone to write code instead of you? Stylesheets will not help you. It is a good point to learn basics of Qt. – Dmitry Sazonov Aug 01 '13 at 15:14
-
What is the question? Anyone knows for sure how it is done. Most likely the one who wrote the Fusion style. But what do **you** want to know? – Greenflow Aug 11 '13 at 18:42
4 Answers
9
I had no idea how such a two colored text could be done, so I checked:
QRegion rightRect = rect;
rightRect = rightRect.subtracted(leftRect);
painter->setClipRegion(rightRect);
painter->setPen(flip ? alternateTextColor : textColor);
painter->drawText(rect,
bar->text,
QTextOption(Qt::AlignAbsolute|
Qt::AlignHCenter|
Qt::AlignVCenter));
if (!leftRect.isNull())
{
painter->setPen(flip ? textColor : alternateTextColor);
painter->setClipRect(leftRect);
painter->drawText(rect,
bar->text,
QTextOption(Qt::AlignAbsolute|
Qt::AlignHCenter|
Qt::AlignVCenter));
}
Basically the text is drawn two times into the same rectangle. Each time with an appropriate clipping.

Abderrahmene Rayene Mihoub
- 1,875
- 3
- 6
- 19

Greenflow
- 3,935
- 2
- 17
- 28
1
From my point of view the best, and probably the easiest, way to do this is to change the pallet for the QProgressBar widget:
QPalette palette = progressBar->palette()
palette.setColor(QPalette::Text, textColor)
palette.setColor(QPalette::HighlightedText, textColor)
progressBar->setPalette(palette)

Alex
- 31
- 3
1
"The setBackgroundRole method let you use a color role for the background, which means one of the predefined color of the style applied to the widget. So your are basically limited to the style and its colors."
Background solution:
value = 65
self.progressBar.setProperty("value", value)
if value < 50:
self.progressBar.setStyleSheet("QProgressBar::chunk { background-color: black; }")
else:
self.progressBar.setStyleSheet("QProgressBar::chunk { background-color: black; } QProgressBar { color: white; }")

Fahri Güreşçi
- 569
- 7
- 14
-3
You can use stylesheet on the Container Widget :
myMainWidget.setStyleSheet(QString("QProgressBar {color: red}"));

rednaks
- 1,982
- 1
- 17
- 23