Originally Posted by
alizadeh91
By behavior qml you don't need to specify its end and start
Of course you do but it is done implicitly. The start value is the current value and the end value is the value being assigned to the property.
So if you have a setter in your class then you just need to create an animation for a smooth transition of the property value, like so:
int MyClass::value() const { return m_value; }
void MyClass::setValue(int x) {
if(value() == x) return;
QVariantAnimation *anim = new QVariantAnimation(this);
connect(anim,
SIGNAL(valueChanged
(QVariant)),
this,
SLOT(valuesetter_internal
(QVariant)));
anim->setStartValue(value());
anim->setEndValue(x);
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
// private slots:
void MyClass
::valuesetter_internal(QVariant v
) { m_value = v.toInt();
emit valueChanged(m_value);
}
int MyClass::value() const { return m_value; }
void MyClass::setValue(int x) {
if(value() == x) return;
QVariantAnimation *anim = new QVariantAnimation(this);
connect(anim, SIGNAL(valueChanged(QVariant)), this, SLOT(valuesetter_internal(QVariant)));
anim->setStartValue(value());
anim->setEndValue(x);
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
// private slots:
void MyClass::valuesetter_internal(QVariant v) {
m_value = v.toInt();
emit valueChanged(m_value);
}
To copy to clipboard, switch view to plain text mode
Then you can just call:
MyClass *obj = ...;
obj->setValue(42);
MyClass *obj = ...;
obj->setValue(42);
To copy to clipboard, switch view to plain text mode
And it will animate to the value requested. You can make it more foul proof by keeping a pointer to the animation object and adjusting the animation if someone calls setValue() before the previous animation reaches the desired value.
Bookmarks