QPropertyAnimation 支持什么 属性 动画?

What property supported in QPropertyAnimation to be animated?

我正在学习 Qt,认为动画按钮很重要。我正在尝试创建我的自定义 QPushButton class,它重新实现鼠标进入和离开事件并使用 qt 动画框架。 QPropertyAnimation

QPropertyAnimation(QObject * target, const QByteArray & propertyName, QObject * parent = 0)

所以如果我想动画化它的大小,我可以使用 "geometry" 作为 属性Name 但是当我想同时更改 text colorbackground color 时让我感到困惑...我应该把什么写成 propertyName?在任何地方都在搜索,但没有找到任何关于 属性 我可以制作动画以及它在 propertyName 构造函数参数中如何命名的文档。

预期答案将是所有可能的参数 Like "geometry" in QtCore.QPropertyAnimation(self,"geometry") (pyqt4/pyside)

我使用 PyQt4,但我也可以使用 PySide 和 Qt4.8 C++,所以在这些库中的任何一个中回答都可以(将全部学习)

编辑:

我发现 this one 使用 "color" 并且它改变了整个按钮颜色(背景颜色),仍然不知道如何让文本可见并为文本颜色设置动画

文本颜色和背景颜色不是 Qt 属性。所以你不能使用 QPropertyAnimation.

为它们设置动画

您可以在官方文档的相应部分找到 QWidget 属性列表:http://doc.qt.io/qt-4.8/qwidget.html

如果您想动态更改一些任意属性,例如文本颜色,您可以启动一个 QTimer 并在连接到定时器 timeout 信号的插槽中执行必要的操作。


--upd--

在 C++ Qt 中你不能动画 'color' 属性 因为没有这样的 属性。如果您尝试这样做,您将在控制台中收到以下警告:

QPropertyAnimation: you're trying to animate a non-existing property color of your QObject


作为 QVariantAnimation 的子类,QPropertyAnimation 不支持所有 QVariant 属性 类型。列出了支持的类型 in the doc:

Not all QVariant types are supported. Below is a list of currently supported QVariant types:

Int
UInt
Double
Float
QLine
QLineF
QPoint
QPointF
QSize
QSizeF
QRect
QRectF
QColor

如果你想为一些其他类型的 属性 设置动画,你应该为它注册一个插值函数:

QVariant cursorInterpolator(const QCursor &start, const QCursor &end, qreal progress)
{
    ...
    return QCursor(...);
}
...
qRegisterAnimationInterpolator<QCursor>(cursorInterpolator);

您可以为您的应用添加 "color" 到 Qt 属性 系统。只需添加代码

Q_PROPERTY(QColor color READ color WRITE setColor)

在 class 小部件中要设置动画并在 class 中添加这两个方法 color()setColor()。 并且不要忘记通过 update() in settter 进行更新。