Search code examples
qttextcolorsqt5qprogressbar

Progress bar with differnet text color around the progress front?


How can one obtain the following effect for text in a QProgressBar from Qt? :

Progress bar text with different colors around the front of the progress bar

The ideea is that I must have a brighter color in the left part of the progress bar.


Solution

  • I would do the custom drawing as follows:

    1. Derive your own progress bar class from QLabel.

    2. Overwrite the paintEvent() function.

    3. In paintEvent(), start drawing with a QPainter:

      • draw the secondary background rectangle
      • draw the text in the secondary color with drawText()
      • draw the first background rectangle (overdrawing first part of text)
      • draw the text in the first color within a rectangle matching the first background rectangle: http://qt-project.org/doc/qt-4.8/qpainter.html#drawText-11

    You should end up with what you want to achieve. Due to Qt's default double buffering you should observe no flicker.