Search code examples
animationqmlqt-quick

QML: How to animate integer (not real)


My problem is that I can't animate integer.

I am showing some result as integer number in Text element, like this:

Text
{
 text: someResult
}

And I have defined behavior:

Behavior on text
{
 NumberAnimation{ duration: 1000; easing.type: Easing.InOutQuad}
}

Problem is that animated text gets real numbers, and I want integers to be.


Example: previous value is 0, and I set new value as 2, this is how animation looks like:

0
0.01
0.05
0.1
0.156
0.36
...
1.81
1.95
2

But what I want to be is:

0
1
2

Solution

  • You can achieve this by explicitly animating an integer property:

    Text {
        property int value: 0
        text: value
        Behavior on value {
            NumberAnimation { duration: 1000; easing.type: Easing.InOutQuad }
        }
    }