偶然的机会发现Qwidget其实可以通过动画的方式也可以做到QML程序一样的过渡效果,使用QPropertyAnimation类。
类QPropertyAnimation
几个重要函数
设置动画运行持续的时间
setDuration(int )
设置运动轨迹
setEasingCurve(const QEasingCurve &)
enum QEasingCurve::Type
{
QEasingCurve::Linear
QEasingCurve::InQuad
QEasingCurve::OutQuad
QEasingCurve::InOutQuad
QEasingCurve::OutInQuad
QEasingCurve::InCubic
QEasingCurve::OutCubic
QEasingCurve::InOutCubic
QEasingCurve::OutInCubic
QEasingCurve::InQuart
QEasingCurve::OutQuart
QEasingCurve::InOutQuart
QEasingCurve::OutInQuart
QEasingCurve::InQuint
QEasingCurve::OutQuint
QEasingCurve::InOutQuint
QEasingCurve::OutInQuint
QEasingCurve::InSine
QEasingCurve::OutSine
…//还有
}
设置动画结束的位置及大小
setEndValue(const QVariant &)
设置动画重复执行的次数
setLoopCount(int )
设置对象名称
setObjectName(const QString &)
设置动画目标属性名
setPropertyName(const QByteArray &)
设置动画开始的位置及大小
setStartValue(const QVariant &)
设置动画目标运行的对象
setTargetObject(QObject *)
停止运行
stop()
开始运行
start()
程序实例
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
startAnimation();
}
void widget::startAnimation()
{
QPropertyAnimation *action = new QPropertyAnimation();
action->setTargetObject(ui->label);
action->setPropertyName("geometry");
action->setEasingCurve(QEasingCurve::InSine);
action->setDuration(500);
action->setLoopCount(3);
action->setStartValue(QRect(QPoint(100,100),QSize(100,100)));
action->setEndValue(QRect(QPoint(300,300),QSize(220,200)));
QTimer *timer= new QTimer;
timer->setInterval(2000);
connect(timer,&QTimer::timeout,[=]{action->start();});
timer->start();
}