从样式表中解析属性值以绘制自定义小部件 [英] Parsing property values from stylesheet for drawing custom widgets

查看:50
本文介绍了从样式表中解析属性值以绘制自定义小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建几个自定义 qt 小部件,其中一些使用自定义绘图.这些小部件应通过公司的 qt 样式表进行样式设置.

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.

这会产生问题,即某些自定义小部件需要从已应用于 QMainWindowQApplication 的样式表中检索值.这个值可以是 qt 的官方属性之一,也可以是一些自定义的 qproperty-... 属性

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

但是,从小部件访问它们并非易事.一种选择是获取样式表字符串并手动解析/正则表达式出我感兴趣的值.这显然是一个糟糕的解决方案,因为 qt 必须已经有一些功能来解析在绘制官方 qt 小部件期间使用的样​​式表数据.

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 创建了一个名为 QStyleSheetStyle 的 QStyle 子类.这意味着您可以通过普通的 QStyle 方法查询样式表信息...

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

问题 1)

这篇博文提出了这个想法,但遗憾的是没有详细说明如何实际实现这一点.由于我缺乏经验,即使深入研究了 qt 的源代码,我也无法找到如何做到这一点.

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.

问题 2)

我认为这仅适用于常规 qt 属性(而不适用于自定义 qproperty-... 属性).与这种方法相比,有没有更好的方法来访问它们?

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)

推荐答案

不,目前无法实现,因为 Qt 样式表的实现是通过作为私有 Qt API 一部分的 QStyle 实现的,在 Qt6 中它旨在暴露那种风格.所以从样式表中获取属性的解决方案是通过qproperty.

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.

这篇关于从样式表中解析属性值以绘制自定义小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆