Skip to content

Instantly share code, notes, and snippets.

@frueter
Created September 14, 2022 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frueter/f9c71905ffe75e5fc17901e7c1f9f066 to your computer and use it in GitHub Desktop.
Save frueter/f9c71905ffe75e5fc17901e7c1f9f066 to your computer and use it in GitHub Desktop.
class PBarDelegate(QtWidgets.QStyledItemDelegate):
def paint(self, painter, option, index):
progress_bar_option = QtWidgets.QStyleOptionProgressBar()
progress_bar_option.rect = option.rect
progress_bar_option.state = QtWidgets.QStyle.State_Enabled
progress_bar_option.direction = QtCore.Qt.LayoutDirection.LeftToRight
progress_bar_option.fontMetrics = QtWidgets.QApplication.fontMetrics()
progress_bar_option.minimum = 0
progress_bar_option.maximum = 100
progress_bar_option.textAlignment = QtCore.Qt.AlignCenter
progress_bar_option.textVisible = True
# set progress and text:
data = index.data(QtCore.Qt.UserRole)
if data:
progress_bar_option.progress = data
progress_bar_option.text = index.data(QtCore.Qt.DisplayRole)
QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_ProgressBar,
progress_bar_option, painter)
@keithel
Copy link

keithel commented Sep 14, 2022

@keithel
Copy link

keithel commented Sep 14, 2022

Ahh.. you use a stylesheet in addition to this, and it's not applying.. It's likely something in your code here that is preventing the stylesheet from having effect.

Take a look at one of the other styles like Plastique and see how they do things with their Progress Bar delegate.

@frueter
Copy link
Author

frueter commented Sep 14, 2022 via email

@keithel
Copy link

keithel commented Sep 23, 2022

It will be in the C++ sources of Qt. (Sorry, it's not implemented in Python - but it should be reasonably understandable even to someone coming from Python, if they're familiar with the Qt API).

Let me see if I can dig it up.

@keithel
Copy link

keithel commented Sep 23, 2022

Oh man I'm dating myself.. They moved the Plastique style out of Qt a long while back I think in the transition from 4.8 to 5.0.
What I think you want to look at is the Fusion style, which is defined here:
https://github.com/qt/qtbase/blob/dev/src/widgets/styles/qfusionstyle.cpp
You'll want to look at drawPrimitive and drawComplexControl.

Actually, this will have you diving into how the call to drawControl you make works, which should show what is happening.

@frueter
Copy link
Author

frueter commented Sep 24, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment