Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QVariantAnimation: how many "steps" given a duration ?



  • Hi all,

    I'm using QVariantAnimation to animate some QGraphicsItems and I'd like to have control over the number of "valueChanged()" signals that will be emitted given some duration.
    I'd like to minimize the number of redraws. For example, I'd like to have no more than 5 redraws (of course it depends on the difference between start and end value, but I'd like to clip it to a maximum of 5).

    Is there some way to set this or another way to simulate it ? I think I can ignore some valueChanged() but for this I need to know the value of the "qreal progress" so I can choose at which time to update the drawing.

    Thanks.


  • Moderators

    @dextermagnific
    if you want to have exactly lets say 5 steps, why not simply starting a QVariantAnimation from 1 to 5 in a given time and do what you want with it on the valueChanged() signal?



  • Well I'm currently interpolating a QColor, and QVariantAnimation is here to hide the details of this interpolation.
    Otherwise I need to call manually the QColor interpolator, which is not part of the API.


  • Moderators

    @dextermagnific
    still possible with a second animation (for example your current animation) and call

    stepAnim->setStartValue( 0 );
    stepAnim->setEndValue( 5 );
    connect(stepAnim, &QVariantAnimation::valueChanged, this, [this](const QVariant &value) {
        colorAnim->keyValueAt( 1.0/stepAnim->endValue().toInt() * value.toInt() );
    });
    


  • Interesting approach.
    But I think keyValueAt() will only return a non null QVariant if a prior setKeyValueAt() has been issued, right ?


  • Moderators

    @dextermagnific
    yes, my example is incorrect.

    Here you go:

    stepAnim->setStartValue( 0 );
    stepAnim->setEndValue( 5 );
    
    //QPropertyAnimation* interpolatorAnim
       interpolatorAnim->setEasingCurve(QEasingCurve::Linear);
       interpolatorAnim->setDuration(stepAnim->endValue());
       interpolatorAnim->setKeyValueAt(0, fromColor);
       interpolatorAnim->setKeyValueAt(1, toColor);
    
    connect(stepAnim, &QVariantAnimation::valueChanged, this, [this](const QVariant &value) {
         interpolatorAnim->setCurrentTime( value.toInt() );
         QColor color = interpolatorAnim->currentValue().value<QColor>();
         ...
    });
    

    You can encapsulate this code in a custom ColorAnimation class if you like to make it more reusable.



  • Thanks. Sounds good. "main" interpolator would be the one doing 0..5 and we get colors from "sub" interpolators.
    This is especially suitable for my use case in which I animate multiple QColors for a single item (outline, fill, text, ....). This will avoid updating drawing each time one of these change. Now I can group multiple changes (outline, fill, text) into one change only.


Log in to reply