Qt文档阅读笔记-QPropertyAnimation官方解析及实例
目录
官方解析
QPropertyAnimation
QPropertyAnimation类为Qt属性提供动画。
QPropertyAnimation类可以修改Qt属性,从而达到动画的效果。这些属性的值存储在QVariants中,所以QPropertyAnimation继承了QvariantAnimation,并且支持他相同元类型的动画,比如他的超类。
这个类必须是QObject的派生类。他必须经过一个设置才能使动画效果成为可能(这样的化QPropertyAnimation能够设置属性的值)。注意到这使得Qt小部件(窗口)都能做出动画的形式。举个例子:
QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0, 0, 100, 30));
animation->setEndValue(QRect(250, 250, 100, 30));
animation->start();
属性名和QObject的实例这属性应该被传递给动画的构造函数。然后你能指定这个属性的开始和结束的指。当你自定义属性的时候实现的效果也是一样的,只要QVariant能支持,QVariantAnimation就能用。
QVariantAnimation类描述了如何去做一个动画的细节。注意,如果start value没有被设置,这个属性会设置一个值,这个值是QPropertyAnimation实例化的时候创建的。
QpropertyAnimation工作的时候是充满魅力的,要想完成一个复杂的动画,要包含几个对象实例,这里把他们放到QAnimationGroup中。一个动画组也是一个动画,这个动画包含了许许多多的动画。当组内的动画运行时他能够管理内部的小动画,查看QParallelAnimationGroup这个例子可以看他如何使用以及管理。
博主例子
我们把官方实例完整的实现出来。可以发现QpropertyAnimation和QTimeLine效果有点像,不过还是有区别的,关于QTimeLine可以看本人的这篇博文:https://mp.****.net/postedit/81530848
运行截图如下:
源码如下:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPropertyAnimation>
#include <QMetaProperty>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QPropertyAnimation *animation=new QPropertyAnimation(ui->pushButton,"minimumSize");
animation->setDuration(10000);
animation->setStartValue(QSize(0,0));
animation->setEndValue(QSize(300,300));
animation->start();
}
Widget::~Widget()
{
delete ui;
}
关于QPushButton的属性可以用下面这种方式获得:
伪代码如下:
for(int i=0;i<ui->pushButton->metaObject()->propertyCount();i++)
qDebug()<<ui->pushButton->metaObject()->property(i).name();
运行截图如下:
后面还有很多,再次不再截图