Archived:How to use QVariantAnimation
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using Qt C++ for the UI) is deprecated.
This article shows how to use QVariantAnimation.
Article Metadata
Tested with
Compatibility
Article
Overview
The Qt Animation framework provides QPropertyAnimation as "default" animator for QObject derived classes. The framework provides also a lower level class: QVariantAnimation. QVariantAnimation cannot be used as it is, since it defines QVariantAnimation::updateCurrentValue as virtual. Since QGraphicsItems are not QObjects, they don't have any qobject property; in this case QVariantAnimation could be used to animate these kind of items directly and avoid subclassing the graphics item itself.
Code
Here is a simple and generic myVariantAnimator class, which emits valueChanged when the value is changed.
#ifndef MYVARIANTANIMATOR_H
#define MYVARIANTANIMATOR_H
#include <QVariantAnimation>
class myVariantAnimator : public QVariantAnimation
{
Q_OBJECT
public:
myVariantAnimator(){};
virtual ~myVariantAnimator(){};
void updateCurrentValue(const QVariant &value){
emit valueChanged(value);
}
};
#endif // MYVARIANTANIMATOR_H
The previous class can be used as follows:
animator.setStartValue(0);
animator.setEndValue(90);
animator.setDuration(500);
animator.setEasingCurve(QEasingCurve::OutQuart);
connect(myVariantAnimator, SIGNAL(valueChanged(const QVariant&)), rcvObj, SLOT(doAnimation()));
In this case, rcvObj::doAnimation will be called as soon as myVariantAnimator::valueChanged is emitted. doAnimation() will be able to animate several items and it's not a problem if it's not a graphics item method.
For more information see: The Animation Framework


(no comments yet)