Changing the color of a QProgressbar()

后端 未结 1 723
时光说笑
时光说笑 2020-12-10 19:30

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 =          


        
相关标签:
1条回答
  • 2020-12-10 19:39

    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)
    
    0 讨论(0)
提交回复
热议问题