Category: How do I do X?
Updated

2023-08-03

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

How do I set font color for progress bar percentage?

Issue

I am unable to set the font color for the progress bar percentage as it defaults to black and there is no widget option for it. It would be great if this feature could be added.

Resolution

As the default progress bar percentage font color is black and there is no widget option to change it, one solution could be to use a custom widget that includes both the progress bar and the percentage text, allowing more flexibility in terms of styling.

To do this, you can create a new widget that inherits from the built-in QProgressBar and overrides the paintEvent() function to draw the percentage text with a font color of your choice. Here's an example implementation:

from PyQt5.QtWidgets import QProgressBarfrom PyQt5.QtGui import QPainter, QColor, QFontclass CustomProgressBar(QProgressBar):    def paintEvent(self, event):        super().paintEvent(event)        # Draw the percentage text with custom font and color        painter = QPainter(self)        font = QFont()        font.setPointSize(12)        font.setBold(True)        painter.setFont(font)        painter.setPen(QColor('#FFA500')) # set to orange color        text = f'{self.value()}%'        painter.drawText(self.rect(), Qt.AlignCenter, text)

Note that you can customize the font and color to your liking by modifying the font and QColor objects respectively in the paintEvent() function.

To use this custom progress bar widget in your code, simply import it and use it like any other widget:

from custom_progress_bar import CustomProgressBar# Create an instance of the custom progress bar widgetprogress_bar = CustomProgressBar()# Set the progress bar valueprogress_bar.setValue(50)# Add the widget to your layoutlayout.addWidget(progress_bar)