Qt 中自定义小部件的自定义样式

发布于 2024-07-24 23:12:40 字数 629 浏览 4 评论 0原文

我想开发一些不完全基于现有绘图基元和子控件的自定义控件。 由于整个应用程序应该是可换肤的,因此我想依赖自定义样式,也可以在样式表上使用。

我需要为这些新控件配置以下内容:

  • 附加指标
  • 附加调色板条目
  • 附加样式选项

我发现我需要从 QStyle 子类之一派生一个新的样式类,并覆盖:

  • 添加新调色板条目的改进方法。
  • drawControl(以及其他绘制方法)用于自定义控件和自定义部件的绘制逻辑。

我有两个问题困扰着我:

  1. 目前,不同的样式有不同的样式类,已经在 Qt 中实现了。 (例如,QMotifStyleQWindowsStyle),每个都有不同的设置。 通过继承,我需要为每种样式重新实现绘画和附加设置逻辑,以便正确集成所有这些样式。 我还有其他选择吗?

  2. 我仍然对样式表如何与这些自定义样式一起使用感到困惑。 任何人都可以指出一个地方,在那里我可以找到比 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:

  1. 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?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-07-31 23:12:40

样式表问题将不会得到解决,因为它不会在自定义类上得到解决。

添加到自定义样式中的额外功能将不会被现有的类理解和处理。 这是因为 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.

内心激荡 2024-07-31 23:12:40
  1. 另一种方法是使用QPalette 获得正确的颜色和 QStyle 到获得正确的间距。

  2. QStyle Qt 4.5 的文档

警告:Qt 样式表当前
不支持自定义 QStyle 子类。
我们计划在未来的版本中解决这个问题。

  1. An alternative could be to use QPalette to get the correct colours and QStyle to get the correct spacing.

  2. QStyle's documentation for Qt 4.5:

Warning: Qt style sheets are currently
not supported for custom QStyle subclasses.
We plan to address this in some future release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文