目录
1.简单介绍
2.使用步骤
3.部分代码示例
4.多项说明
5.信号反馈
6.自定义属性
1. 定义自定义属性
2. 使用 QPropertyAnimation 动画化自定义属性
3. 连接信号和槽
4.注意事项
7.更多高级示例
QPropertyAnimation是Qt中的一个类,用于实现属性动画效果。它通过改变对象的属性值来创建动画效果,可以实现平移、旋转、缩放等动画效果。QPropertyAnimation继承自QAbstractAnimation类,因此可以和其他动画类一起使用,同时也可以结合QAnimationGroup来实现多个动画的组合效果。
QPropertyAnimation的核心是通过插值器(Interpolator)和时间线(Timeline)来控制属性值的变化。插值器决定了属性值在时间线上的变化规律,而时间线则决定了动画的时长和速度。
QPropertyAnimation可以作用于任何QObject的派生类,包括QWidget、QGraphicsItem等。它可以控制对象的任何可变属性,只要属性可以通过setter和getter方法来获取和设置。
使用QPropertyAnimation可以轻松地为应用程序添加动画效果,提升用户体验。无论是在桌面应用程序还是移动应用程序中,都可以使用QPropertyAnimation来创建各种各样的动画效果。
使用 QPropertyAnimation
的基本步骤:
创建动画对象:首先,你需要创建一个 QPropertyAnimation
对象,并将其与要动画化的属性相关联。
设置目标对象和属性:你需要指定动画的目标对象和要动画化的属性。
设置动画参数:包括动画的起始值、结束值、持续时间、缓动函数等。
启动动画:最后,通过调用 start()
方法来启动动画。
void MainWindow::initUi()
{
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
connect(ui->pushButton, &QPushButton::clicked,this, &MainWindow::slotsclicked);
}
void MainWindow::slotsclicked()
{
static int resizeValue{100};
resizeValue = -resizeValue;
QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(500); // 持续时间
//运动轨迹
animation->setStartValue(QRect(pos().x(), pos().y(), width(), height()));
//animation->setEndValue(QRect(pos().x() + resizeValue, pos().y() + resizeValue, width(), height()));
animation->setEndValue(QRect(pos().x() + width(), pos().y(), 0, height()));
animation->setEasingCurve(QEasingCurve::InQuad);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
1.关于setEasingCurve其实有更多的运动类型,可以搜索QEasingCurve
QPropertyAnimation *animation = new QPropertyAnimation(&object, "propertyName");
在这个例子中,
"propertyName"
应该替换为你想要动画化的属性的实际名称。object
是动画的目标对象,它应该是一个QObject
的实例。Qt 使用属性系统来管理对象的状态,每个属性都有一个名称,可以通过
QObject::setProperty()
和QObject::property()
方法来设置和获取。QPropertyAnimation
利用这个系统来动态地改变属性值。
for(int i=0;ipushButton->metaObject()->propertyCount();i++)
qDebug()<pushButton->metaObject()->property(i).name();
下面是一些常见的属性名称示例:
"geometry"
:用于动画化 QWidget
或其子类对象的几何形状。"pos"
:用于动画化 QWidget
或其子类对象的位置。"size"
:用于动画化 QWidget
或其子类对象的大小。"x"
、"y"
:用于动画化 QWidget
或其子类对象在父对象坐标系中的 x 或 y 坐标。"opacity"
:用于动画化 QWidget
或其子类对象的不透明度。上面提到要动画化的属性相关联,如果上面代码修改成opacity,就无法工作
在 QPropertyAnimation
中,你可以监听以下几个回调函数来处理动画完成后的事件:
finished
信号:当动画完成时发出。这个信号不传递任何参数。
connect(animation, &QPropertyAnimation::finished, this, &MyClass::onAnimationFinished);
在 MyClass::onAnimationFinished
槽函数中,你可以执行动画完成后需要进行的操作。
valueChanged
信号:每当动画的当前值改变时发出。这个信号传递一个 QVariant
参数,表示动画的当前值。
connect(animation, &QPropertyAnimation::valueChanged, this, &MyClass::onValueChanged);
在 MyClass::onValueChanged
槽函数中,你可以获取动画的当前值并根据需要进行处理。
started
信号:当动画开始时发出。这个信号不传递任何参数。
connect(animation, &QPropertyAnimation::started, this, &MyClass::onAnimationStarted);
在 MyClass::onAnimationStarted
槽函数中,你可以执行动画开始时需要进行的操作。
stopped
信号:当动画被停止时发出。这个信号不传递任何参数。
connect(animation, &QPropertyAnimation::stopped, this, &MyClass::onAnimationStopped);
在 MyClass::onAnimationStopped
槽函数中,你可以执行动画停止时需要进行的操作。
stateChanged
信号:当动画的状态改变时发出。这个信号传递两个参数:QAbstractAnimation::State
表示新状态,QAbstractAnimation::State
表示旧状态。
connect(animation, &QPropertyAnimation::stateChanged, this, &MyClass::onAnimationStateChanged);
在 MyClass::onAnimationStateChanged
槽函数中,你可以根据动画的新旧状态执行相应的操作。
这些信号允许你在动画的不同阶段执行自定义的逻辑,例如更新用户界面、处理动画完成后的清理工作或者触发其他动画。
Qt中,如果你想要动画化一个不是由Qt框架预定义的属性,你可以使用
QVariant
属性系统来自定义属性。自定义属性允许你为任何QObject
派生类添加新的属性,并通过QPropertyAnimation
来动画化这些属性。
以下是如何为自定义类添加属性并使用 QPropertyAnimation
来动画化这些属性的步骤:
首先,你需要在你的类定义中使用 Q_PROPERTY
宏来声明自定义属性。这通常在类的头文件中完成。
#include
#include
class MyClass : public QObject {
Q_OBJECT
Q_PROPERTY(int customInt READ customInt WRITE setCustomInt NOTIFY customIntChanged)
Q_PROPERTY(QColor customColor READ customColor WRITE setCustomColor NOTIFY customColorChanged)
public:
MyClass(QObject *parent = nullptr) : QObject(parent) {}
int customInt() const { return m_customInt; }
void setCustomInt(int value) { m_customInt = value; emit customIntChanged(value); }
QColor customColor() const { return m_customColor; }
void setCustomColor(const QColor &value) { m_customColor = value; emit customColorChanged(value); }
signals:
void customIntChanged(int newValue);
void customColorChanged(const QColor &newValue);
private:
int m_customInt;
QColor m_customColor;
};
QPropertyAnimation
动画化自定义属性一旦你定义了自定义属性,你可以像动画化任何其他属性一样动画化它们。
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MyClass myObject;
QPropertyAnimation *animation = new QPropertyAnimation(&myObject, "customInt");
animation->setStartValue(0);
animation->setEndValue(100);
animation->setDuration(2000);
animation->start();
return app.exec();
}
你还可以连接自定义属性的更改信号到槽函数,以便在属性值改变时执行某些操作。
connect(&myObject, &MyClass::customIntChanged, [](int newValue) {
qDebug() << "Custom int changed to:" << newValue;
});
QVariant
可以存储的类型。对于不支持的类型,你可能需要使用 QVariant
来包装它们。Q_PROPERTY
宏时,确保你提供了 READ
、WRITE
和 NOTIFY
宏,这样Qt的属性系统才能正确地处理它们。示例1 示例2 示例3