真正的梦就是现实的彼岸

Real dream is the other shore of reality
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Qt 动画框架

Posted on 2016-04-15 15:42  spplus  阅读(488)  评论(0编辑  收藏  举报

  最近一个项目中的需要做动画效果,很自然就想起来用qt animation framework 。这个框架真的很强大。支持多个动画组合,线性动画,并行动画等。在此总结一下使用该框架一些需要注意的地方:

  一:动画的target必须继承至 QObject类,如果存在多继承,QObject 必须为第一继承类。

  二:如果使用QPropertyAnimation ,必须注意该对象的生命周期。我就在此出被卡了很长时间。比如我是在 graphicscene中的某一槽函数中进行展示动画的,直接在响应函数中加上如下代码:

QPushButton button("Animated Button");
 button.show();

 QPropertyAnimation animation(&button, "geometry");
 animation.setDuration(10000);

 animation.setKeyValueAt(0, QRect(0, 0, 100, 30));
 animation.setKeyValueAt(0.8, QRect(250, 250, 100, 30));
 animation.setKeyValueAt(1, QRect(0, 0, 100, 30));

 animation.start();

动画效果怎么也出不来。后来才发现,这个局部定义的animation在方法执行完后,生命周期结束,但是动画还没有完成。

所以正确的做法:

m_animation.setTargetObject(m_curItem);
    m_animation.setPropertyName("pos");
    m_animation.setDuration(2000);
    m_animation.setStartValue(QPointF(0,0));
    m_animation.setEndValue(m_curItem->pos());
    m_animation.setEasingCurve(QEasingCurve::OutBounce);
    m_animation.start();

声明一个类变量m_animation,或者动态new一个 QPropertyAnimation ,但是这个new的对象何时释放,也是一个问题。

建议采用第一种方式。

三:动画支持组动画,状态机模式动画,和graphicsview 框架完美集成