How to set the duration of a QVariantAnimation constant to different values?
-
How to get a constant animation duration time, independent of how small/bigger the frame size is?
For example, when startSize is 900x900, the animation duration time should be the same than when the startSize be 100x100
void ThumbnailFrame::resetSize() { QVariantAnimation* ani = new QVariantAnimation; QSize startSize = thumbnailFrame->size(); QSize endSize(p_ui->displayWidth->text().toInt(), p_ui->displayHeight->text().toInt()); qreal distance = qSqrt(qPow(startSize.width() - endSize.width(), 2) + qPow(startSize.height() - endSize.height(), 2)); int duration = static_cast<int>(distance / 2); // factor ani->setEasingCurve(QEasingCurve::Type::Linear); ani->setDuration(duration); ani->setStartValue(startSize); ani->setEndValue(endSize); connect(ani, &QVariantAnimation::valueChanged, thumbnailFrame, [=](const QVariant& value) { thumbnailFrame->setFixedSize(value.toSize()); }); ani->start(QAbstractAnimation::DeleteWhenStopped); }
I tried
qreal distance = qSqrt(qPow(startSize.width() - endSize.width(), 2) + qPow(startSize.height() - endSize.height(), 2)); int duration = static_cast<int>(distance / 2); // factor
But it aint resulting in a constant duration speed independent of the frame start/end size
-
Hi @Kattia ,
I think you're mixing two approaches.
How to get a constant animation duration time, independent of how small/bigger the frame size is?
Here you are asking for "constant time", independent of covered distance, which needs to result in variable speeds.
But it aint resulting in a constant duration speed independent of the frame start/end size
and here you are saying, that you expect the above approach to have constant speed, no matter if you animate from 900x900 or 100x100 to whatever size.
How should this work?! :)
You can't drive 1h with 100km/h twice and expect to cover 100km in the first and 500km in the second run :))It's either... or...
Either you set a fixed duration and let theQVariantAnimation
calculate the step-size fromstart
toend
("speed").
Or you calculate the step-size ("speed") on your own before and set the resulting duration time. -
@Kattia said in How to set the duration of a QVariantAnimation constant to different values?:
ani->setDuration(duration);
If you set this to the time you need, it should be enough.
QVariantAnimation
transformsstart
toend
using the curve function given hereani->setEasingCurve(QEasingCurve::Type::Linear);
in a constant time interval.
-