Changing the color of a QProgressbar()
Asked Answered
S

1

4

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.setParent(Frame1)
Pbar1.setGeometry(0, 0, 306, 30)
Pbar1.setValue(Frame1Value)
if Pbar1.value == 100
......Pbar1.setColor(Red)

Frame1Value is dependable on some early calculations, and to be assumed never calculates to exactly the same value.

I would like the progressbar to turn 'red' when the value is 100 (which the value is set limited at since a progressbar won't show values above 100%), so giving me a better visual image of the fact that the value is 'out of constraint'.

I am aware that .setColor isn't a known command for a Progressbar, but it's just to show my idea.

Does anyone know how to do this, or if it is even possible??

Thx in advance!

Swear answered 8/6, 2011 at 10:29 Comment(1)
Probably not easily, because the display of the progress bar depends on the Qt theme.Voelker
U
9

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;
}
"""

class MyProgressBar(QtGui.QProgressBar):
    def __init__(self, parent = None):
        QtGui.QProgressBar.__init__(self, parent)
        self.setStyleSheet(DEFAULT_STYLE)

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

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

unfinished completed

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)
Unhelm answered 8/6, 2011 at 12:41 Comment(4)
imageshack.us/photo/my-images/135/unledjqc.png I used to have my progressbars looking like on the picture i added on imageshack. This i did by putting this line of code somewhere: >QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique')) Is it possible to change the style of that what you proposed into the one i have? I just like mine more... Doesn't take the fact away that your answer indeed works fine. I'd be totally happy if you could help me with this also, otherwise thanks for your already satisficing answer.Swear
I have edited my answer. BTW QtGui.QApplication.setStyle('Plastique')is shorter and works as well ;)Unhelm
My question was a little but unnecessairy... I just had the 'crazy' idea that I could just change the way you filled in the styles. So i just edited the colors it should use and change the chunk margin to 0px so a solid fill is aquired. What i am wondering however, is there a site with all the possible colors i could use for a style?Swear
Just vaguely wondering why the existing pallet accessible as 'self.palette()' cannot be used to set the color? Why does a new palette have to be created and set? Thanks.Vine

© 2022 - 2024 — McMap. All rights reserved.