Parsing property values from stylesheet for drawing custom widgets

一笑奈何 提交于 2021-02-05 11:20:07

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/65055215/parsing-property-values-from-stylesheet-for-drawing-custom-widgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!