位置和大小动画
this->resize(500,500); QLabel* label=new QLabel("动画",this); label->move(10,10); label->setStyleSheet("background-color: rgb(255, 251, 100)"); //设置背景色 QPropertyAnimation *animation = new QPropertyAnimation(label, "geometry"); //创建属性动画对象 //参数1:动画作用的QObject对象;也可以用setTargetObject函数来设置 //参数2:要用来动画的属性;也可以用setPropertyName函数来设置 animation->setDuration(10000); //设置这个动画的时长--单位:毫秒 int n=animation->duration(); //返回动画的作用时间--单位:毫秒 animation->setStartValue(QRect(0, 0, 200, 200)); //设置动画的起始坐标与大小 animation->setKeyValueAt(0.4, QRect(20, 250, 20, 30)); //在40%的位置插入 animation->setEndValue(QRect(450, 450, 100, 30)); //设置动画的结束坐标与大小 animation->setEasingCurve(QEasingCurve::OutBounce); //设置动画效果 //QEasingCurve枚举值参看assistant.exe animation->start(); //动画开始
动画组
this->resize(500,500); QLabel* label=new QLabel("AAA",this); QLabel* label1=new QLabel("BBB",this); label->setStyleSheet("background-color: rgb(255, 0, 0)"); label1->setStyleSheet("background-color: rgb(0, 0, 255)"); QPropertyAnimation *animation = new QPropertyAnimation(label, "geometry"); QPropertyAnimation *animation1 = new QPropertyAnimation(label1, "geometry"); animation->setDuration(10000); animation1->setDuration(10000); animation->setStartValue(QRect(0, 0, 100, 30)); animation->setEndValue(QRect(450, 450, 100, 30)); animation1->setStartValue(QRect(450, 450, 100, 30)); animation1->setEndValue(QRect(0, 0, 100, 30)); /*动画组分为两种分别为串行和并行,对应于QAnimationGroup的两个子类QSequentialAnimationGroup和QParallelAnimationGroup 串行动画第一个执行完毕才会执行第二个;并行动画,一起执行 需要 #include <QSequentialAnimationGroup> 或#include <QParallelAnimationGroup> */ QSequentialAnimationGroup* group=new QSequentialAnimationGroup; //创建动画组 group->addAnimation(animation); //添加动画 group->addAnimation(animation1); group->start(); //启动动画组