-
-
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) |
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.
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.
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.
I typically do this with stylesheets:
https://gist.github.com/keithel/dc8845ef46a89f5fc317db26e57adce0