Skip to content Skip to sidebar Skip to footer

Changing The Color Of A Qprogressbar()

I was wondering whether it's possible to change the color of a PyQt Progressbar? I have the following code: from PyQt4 import QtGui, QtCore Pbar1 = QtGui.QProgressBar() Pbar1.setPa

Solution 1:

You can sublass QProgressBar and use some style sheet see Customizing Qt Widgets Using Style Sheets and Customizing QProgressBar:

from PyQt4 import QtGui, QtCore

DEFAULT_STYLE = """
QProgressBar{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center
}

QProgressBar::chunk {
    background-color: lightblue;
    width: 10px;
    margin: 1px;
}
"""

COMPLETED_STYLE = """
QProgressBar{
    border: 2px solid grey;
    border-radius: 5px;
    text-align: center
}

QProgressBar::chunk {
    background-color: red;
    width: 10px;
    margin: 1px;
}
"""classMyProgressBar(QtGui.QProgressBar):
    def__init__(self, parent = None):
        QtGui.QProgressBar.__init__(self, parent)
        self.setStyleSheet(DEFAULT_STYLE)

    defsetValue(self, value):
        QtGui.QProgressBar.setValue(self, value)

        if value == self.maximum():
            self.setStyleSheet(COMPLETED_STYLE)

unfinishedcompleted

Another solution would be to reassign a palette to the QProgressBar which will allow you to have a "style aware" component:

class MyProgressBar(QtGui.QProgressBar):
    def setValue(self, value):
        QtGui.QProgressBar.setValue(self, value)
        if value == self.maximum():
            palette = QtGui.QPalette(self.palette())
            palette.setColor(QtGui.QPalette.Highlight, 
                             QtGui.QColor(QtCore.Qt.red))
            self.setPalette(palette)

Post a Comment for "Changing The Color Of A Qprogressbar()"