Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. DELEGATE: how to change color to a progress bar?
Forum Updated to NodeBB v4.3 + New Features

DELEGATE: how to change color to a progress bar?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 1 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZioLupo
    wrote on last edited by
    #1

    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?
    P.S.
    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
    ZioLupo

    from PySide2 import QtCore, QtGui, QtWidgets
    
    class ProgressDelegate(QtWidgets.QStyledItemDelegate):
        def paint(self, painter, option, index):
            progress = index.data(QtCore.Qt.UserRole+1000)
            opt = QtWidgets.QStyleOptionProgressBar()
            opt.textAlignment=QtCore.Qt.AlignCenter
            opt.rect = option.rect
            opt.minimum = 0
            opt.maximum = 100
            opt.progress = progress
            opt.text = "{}%".format(progress)
            opt.textVisible = True
            palette = opt.palette
            palette.setColor(QtGui.QPalette.Highlight, QtCore.Qt.red)
            opt.palette=palette
    
            QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_ProgressBar, opt, painter)
    
    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZioLupo
      wrote on last edited by
      #2

      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:
      https://forum.qt.io/topic/28077/solved-qstyleoptionprogressbar-and-style-sheet

      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 = index.data(QtCore.Qt.UserRole+1000)
              if (progress >= 0):
                  rect=QtCore.QRect(option.rect.x()+5, option.rect.y()+5, option.rect.width(),
                          option.rect.height()-10)
                  painter.save()
                  painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
                  painter.setBrush(QtGui.QColor(75,75,75)) #grigio molto scuro
                  painter.setPen(QtGui.QColor("transparent"))
                  rect2=QtCore.QRect(option.rect.x()+3, option.rect.y()+3, option.rect.width()-5,
                              option.rect.height()-7)
                  painter.drawRect(rect2)
                  progBarWidth = float((option.rect.width() * progress) / 100)
                  if (progBarWidth > 5):
                      # Painting the ProgressBar
                      painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
                      painter.setBrush(QtGui.QColor(0,122,204))
                      rect5=QtCore.QRect(option.rect.x()+3, option.rect.y()+3, progBarWidth-5,
                                  option.rect.height()-7)
                      painter.drawRect(rect5)
                  painter.setPen(QtGui.QColor(QtCore.Qt.white))
                  painter.drawText(rect2, QtCore.Qt.AlignCenter, str(progress)+"%")
                  painter.restore()
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved