Qt 中自定义小部件的自定义样式
我想开发一些不完全基于现有绘图基元和子控件的自定义控件。 由于整个应用程序应该是可换肤的,因此我想依赖自定义样式,也可以在样式表上使用。
我需要为这些新控件配置以下内容:
- 附加指标
- 附加调色板条目
- 附加样式选项
我发现我需要从 QStyle
子类之一派生一个新的样式类,并覆盖:
- 添加新调色板条目的改进方法。
drawControl
(以及其他绘制方法)用于自定义控件和自定义部件的绘制逻辑。
我有两个问题困扰着我:
目前,不同的样式有不同的样式类,已经在 Qt 中实现了。 (例如,
QMotifStyle
、QWindowsStyle
),每个都有不同的设置。 通过继承,我需要为每种样式重新实现绘画和附加设置逻辑,以便正确集成所有这些样式。 我还有其他选择吗?我仍然对样式表如何与这些自定义样式一起使用感到困惑。 任何人都可以指出一个地方,在那里我可以找到比 Qt 文档提供的信息更多的信息吗? (Qt 中的示例对我帮助不大)。
I want to develop some custom controls that are not based entirely on existing drawing primitives and sub-controls. Since the entire application should be skinnable, I want to rely on custom styles, possible on style sheets as well.
What I need to configure for those new controls are the following:
- Additional metrics
- Additional palette entries
- Additional style options
I found out that I need to derive a new style class from one of the QStyle
subclasses, and override:
- polish method for adding new palette entries.
drawControl
(and the other draw methods) for painting logic for custom control and custom parts.
I have two issues that bother me:
Currently, there are different style classes for different styles, already implemented in Qt. (e.g.,
QMotifStyle
,QWindowsStyle
), each with different settings. Through inheritance, I would need to re-implement the painting and additional setup logic for each style, in order to properly integrate in all these styles. Do I have another alternative?I am still confused about how can style sheets be used with these custom styles. Can anybody point to a place where I can find more information than the one provided by Qt documentation? (The samples in Qt don't help me too much).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
样式表问题将不会得到解决,因为它不会在自定义类上得到解决。
添加到自定义样式中的额外功能将不会被现有的类理解和处理。 这是因为 C++ 是一种静态语言,没有(干净且合理的)方法来对运行时类进行猴子修补。 一个潜在的解决方案是使用包装标准 QStyle 子类的某个实例的代理样式。 根据您想用它实现的程度,您可以参考两篇文章:跨平台代码和样式 和 观看“感受”问答 一个。
如果我是你,我不会采用 QStyle 方法。 毕竟,您创建自定义小部件(例如,
FooSomething
),因此您也可以添加创建完全不同的自定义样式(例如,FooStyle),它甚至不需要模仿 QStyle。 当然,那么您仍然需要复制类似的功能,例如支持样式表。The style sheet problem will not be solved, as it will not on custom classes.
The extra goodies added to a custom style will not be understood and taken care by already existing classes. This is because C++ is a static language and there is no (clean and sane) way to monkey-patch the run-time classes. A potential solution is to use a proxy style which wraps a certain instance of standard QStyle subclasses. Depending on how much you want to achieve with it, you can refer to two articles: Cross-platform code and styles and Look 'n' Feel Q & A.
If I were you, I would not go with the QStyle approach. After all, you create custom widgets (e.g.,
FooSomething
) so you can as well add create completely different custom styles (e.g., FooStyle), it does not even need to mimic QStyle. Of course, then you still need to replicate similar functionalities, e.g., to support style sheet.另一种方法是使用
QPalette
获得正确的颜色和QStyle
到获得正确的间距。QStyle
Qt 4.5 的文档:An alternative could be to use
QPalette
to get the correct colours andQStyle
to get the correct spacing.QStyle
's documentation for Qt 4.5: