如何在 Qt4 中为小部件透明度设置动画?

发布于 2024-09-27 13:43:12 字数 289 浏览 0 评论 0原文

我需要为小部件透明度设置动画。 在我看来,我应该使用 QPropertyAnimation。但我如何定义小部件半透明呢?我应该使用这样的东西吗?

I need to animate widget transparency.
It seems to me, that I should use QPropertyAnimation. But how could i define widget translucent? Should I use something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

余生一个溪 2024-10-04 13:43:12

至少从我玩过的内容来看,您的问题有两个子答案:

  1. 如果您想设置 QWidget 顶级窗口的不透明度动画,您可以在“windowOpacity”值上使用从 0 到 1 的 QPropertyAnimation。您可能想检查确切的语法,因为我使用的是 PyQt。
  2. 如果您想要为按钮或文本标签的不透明度设置动画,则此属性将不起作用,您需要使用 QGraphicsWidgets。它们继承了 QGraphicsItem,因此它们具有不透明度。不幸的是,这意味着使用代理小部件 QGrapgicsProxyWidget 来保持普通小部件与其图形小部件对应物同步,因为 QGraphicsWidget 不继承 QWidget。要了解更多信息,请查看动画框架示例,特别是有关如何使用代理小部件的状态机。

对于 windowOpacity 动画:

QPropertyAnimation animate = new QPropertyAnimation(this, "windowOpacity", this);

animate.setDuration(100);动画.setStartValue(1); animate.setEndValue(0);
animate.start();

这仅适用于支持某种复合扩展的系统。

希望有帮助!

There are two sub-answers to your question, from what I've played with at least:

  1. If you want to set animate the opacity of a QWidget toplevel window, you can use QPropertyAnimation from 0 to 1 on the "windowOpacity" value. You might want to check the exact syntax since I'm using PyQt.
  2. If you want to animate the opacity of a button or of a text label, this property won't work and you need to use QGraphicsWidgets. They inherit QGraphicsItem and so, they have opacity. Unfortunately, this means using proxy widgets QGrapgicsProxyWidget to keep normal widgets in sync with their graphics widget counterparts since QGraphicsWidget doesn't inherit QWidget. To find out more, check out the Animation Framework Examples, in particular the State Machine for how to use proxy widgets.

For windowOpacity animations:

QPropertyAnimation animate = new QPropertyAnimation(this, "windowOpacity", this);

animate.setDuration(100); animate.setStartValue(1); animate.setEndValue(0);
animate.start();

This will work only on systems that support some kind of Composite extension.

Hope it helps!

十年不长 2024-10-04 13:43:12

您可以尝试使用 windowOpacity 属性QWidget。如果这不起作用,您可能需要定义自己的属性并在小部件的绘制事件中使用它。

You could try using the windowOpacity property in QWidget. If that doesn't work, you may need to define your own property and use it in the paint event for your widget.

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