Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QVariantAnimation: how many "steps" given a duration ?

    General and Desktop
    2
    7
    140
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dextermagnific last edited by

      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.

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @dextermagnific last edited by

        @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?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • D
          dextermagnific last edited by

          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.

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @dextermagnific last edited by

            @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() );
            });
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 1
            • D
              dextermagnific last edited by

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

              raven-worx 1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators @dextermagnific last edited by raven-worx

                @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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply Reply Quote 2
                • D
                  dextermagnific last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post