pythonpyside2qtstylesheetsqproperty

Parsing property values from stylesheet for drawing custom widgets


I have to create a couple of custom qt widgets, some of which use custom drawing. Those widgets should be styled via the companies qt stylesheet.

This creates the problem that some custom widget need to retrieve a value from stylesheet that has been applied to the QMainWindow or QApplication. This value could either be one of qt's official properties or some custom qproperty-... property

However, it is not trivial to access them from the widget. One option would be to get the stylesheet string and manually parse/regex out the values I am interested in. This is obviously a terrible solution, as qt must already have some functionality to parse the stylesheet data which is used during drawing the official qt widgets.

... Qt creates a QStyle sub-class called QStyleSheetStyle.That means you can query style sheet information via the normal QStyle methods ...

Problem 1)

This post addresses this idea, but unfortunately doesn't go into detail how to actually achieve this. With my lack of experience, I wasn't able to find out how to do this, even after diving into qt's source.

Problem 2)

I assume that this would only apply to regular qt properties (and not custom qproperty-... properties). Is there a better way to access them compared to this approach?

# example for retrieving qproperty-offset
def get_offset(self):
    return self._offset

def set_offset(self, offset:int):
    self._offset = offset
    self.update()

offset = QtCore.Property(int, get_offset, set_offset)

Solution

  • No, it is currently not possible since the implementation of Qt Style Sheet is through a QStyle that is part of the private Qt API, in Qt6 it is intended to expose that style. So the solution to get properties from the stylesheet is through qproperty.