DELEGATE: how to change color to a progress bar?

  • Hi all,
    I have a small programs that is copying file between two remotes system using SSH.
    I'm monitoring the progress, but I would like to change the colors of the color bars in the program.

    I have a Qtableview with a QStandardItemModel and inside a column with the help of a delegate I have a progressbar. Everything is working fine.
    But I would like to change color and I'm not able to do it. As far as I know stylesheets are not working with delegate (BTW I tried and I can confirm: they are not working!).
    So I wrote the following code but it's not working.
    Someone can help?
    As a second step I would like to have a static progressbar. For static I means a bar that is not changing color simulating the movement. This is the effect I want to avoid:
    alt text

    Thank you so much

    from PySide2 import QtCore, QtGui, QtWidgets
    class ProgressDelegate(QtWidgets.QStyledItemDelegate):
        def paint(self, painter, option, index):
            progress =
            opt = QtWidgets.QStyleOptionProgressBar()
            opt.rect = option.rect
            opt.minimum = 0
            opt.maximum = 100
            opt.progress = progress
            opt.text = "{}%".format(progress)
            opt.textVisible = True
            palette = opt.palette
  , opt, painter)

  • Investigating further I was not able to find anything for changing color and stopping animation in my delegate.
    So I tried to look for a different approach: writing my own rectangle as it was a progressbar.
    I was able to find this post:

    and I decided to follow this approach for my python program.

    Here it is my python simplified version (with added text with percentage) so if someone has this problem can use this approach.

    I hope this can help.
    BTW: this is not really like solving my problem but it is a wonderful workaround!

    class ProgressDelegate(QtWidgets.QStyledItemDelegate):
        def paint(self, painter, option, index):
            progress =
            if (progress >= 0):
                rect=QtCore.QRect(option.rect.x()+5, option.rect.y()+5, option.rect.width(),
                painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
                painter.setBrush(QtGui.QColor(75,75,75)) #grigio molto scuro
                rect2=QtCore.QRect(option.rect.x()+3, option.rect.y()+3, option.rect.width()-5,
                progBarWidth = float((option.rect.width() * progress) / 100)
                if (progBarWidth > 5):
                    # Painting the ProgressBar
                    painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
                    rect5=QtCore.QRect(option.rect.x()+3, option.rect.y()+3, progBarWidth-5,
                painter.drawText(rect2, QtCore.Qt.AlignCenter, str(progress)+"%")

Log in to reply