Joshua Leung
Joshua Leung

Reputation: 169

Dynamically change from/to values used in animations

Say I've got the following code:

Rectangle {
  id: rect

  property int minVal : 0
  property int maxVal : 10
  property int duration: 1000

  SequentialAnimation {
    loops: Animation.infinite
    NumberAnimation {
       target: rect; property: "x"
       from: minVal;  to: maxVal
       duration: duration
    }
    NumberAnimation {
       target: rect; property: "x"
       from: maxVal;  to: minVal
       duration: duration
    }
  }
}

Then later, I hook up this object's minVal, maxVal, and duration properties to sliders, so that I can play around with this effect.

Is there any way to force the animation to use the new values for min/maxVal?

Currently, it seems that the minVal/maxVal values present when the animations were initialised get "baked" into the animations, with no way to force these to get recalculated/rebaked later

Previously I've been able to get away with throwing away the entire QML canvas everytime one of these changes was needed. But in this case, it's not really practical, so I was wondering if there were other alternatives?

Upvotes: 2

Views: 1509

Answers (1)

derM
derM

Reputation: 13691

I could not find a way to 'bake' in those values. For me, at least, this works as I expected:

Button {
    id: but
    property int count: 0
    property int minX: 0
    property int maxX:  1000

    text: 'start' + x

    onCountChanged: {
        minX += 10
        maxX -= 10
    }

    onClicked: ani.start()
}

SequentialAnimation {
    id: ani
    NumberAnimation {
        target: but
        property: 'x'
        from: but.minX
        to: but.maxX
    }
    ScriptAction {
        script: { but.count += 1 }
    }
}

I tried as much as possible to avoid bindings that could be baked in somehow.
Have I missed your problem?

Upvotes: 2

Related Questions