How to change the color of a QProgressBar text with its value?

I don’t know how to partially change the color of the text in the progress bar when its value becomes almost 50%. This effect appears automatically in the execution line of the merge style (figure below). Does anyone know how to do this?

Fusion style progress bar

+7
c ++ qt progress-bar
source share
4 answers

It is too lazy to write a working code example, and even more so to take a screenshot. Not even for 50 repetitions. :-)

However, the question was somewhat interesting. I had no idea how to make such a two-color text. So I checked: http://qt.gitorious.org/qt/qtbase/blobs/stable/src/widgets/styles/qfusionstyle.cpp Line 1450ff ( http://qt.gitorious.org/qt/qtbase/blobs/ stable / src / widgets / styles / qfusionstyle.cpp # line1450 ).

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 twice in the same rectangle. Each time with the appropriate clipping. Easy if you know how to do it. :-)

+8
source share

From my point of view, the best and perhaps the easiest way to do this is to change the pan for the QProgressBar widget:

  QPalette palette = progressBar->palette() palette.setColor(QPalette::Text, textColor) palette.setColor(QPalette::HighlightedText, textColor) progressBar->setPalette(palette) 
0
source share

Hi guys, I have the same question, I know it was a long time ago when he was asked. but I need help, I tried to use your method, but my program is in love

need help thanks

  void Q2ProgressBar::paintEvent(QPaintEvent* /* event */) { QRegion rightRect(rect()); QRegion leftRect(rect()); QPainter* painter; rightRect = rightRect.subtracted(leftRect); painter->setClipRegion(rightRect); painter->setPen(text()=="50%" ? "red" : "green"); painter->drawText(rect(), text(), QTextOption(Qt::AlignAbsolute| Qt::AlignHCenter| Qt::AlignVCenter)); if (!leftRect.isNull()) { painter->setPen(text()=="50%" ? "green" : "red"); painter->setClipRect(leftRect); painter->drawText(rect(), text(), QTextOption(Qt::AlignAbsolute| Qt::AlignHCenter| Qt::AlignVCenter)); } } 

this is the same code as the preview

0
source share

You can use the stylesheet in the container widget:

 myMainWidget.setStyleSheet(QString("QProgressBar {color: red}")); 
-3
source share

All Articles