Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Set property and ignore behavior animation
QtWS25 Last Chance

Set property and ignore behavior animation

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlanimationnumberanimationbehavior
3 Posts 2 Posters 1.7k Views
  • 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.
  • A Offline
    A Offline
    Alexander_Lanin
    wrote on 29 Nov 2015, 23:52 last edited by A Former User
    #1

    Hi there,

    I'm trying to animate a property towards one direction but not towards the other.
    Animation usually goes downwards, while setting to zero is a reset function.
    Here is minimal example which obviously animates on both directions, but just to get the idea.

    Rectangle {
        height: 200
        width: 200
    
        Rectangle {
            id: icon
            height: 20
            width: 200
            color: "red"
    
            Behavior on y { NumberAnimation {} }
        }
    
        MouseArea {
            onClicked: icon.y += 20;
            onDoubleClicked: icon.y = 0; // this shall not be animated
            anchors.fill: parent
        }
    }
    

    I was able to work around this restriction with the following code:

    Rectangle {
        height: 200
        width: 200
    
        Rectangle {
            id: icon
            height: 20
            width: 200
            color: "red"
    
            onYChanged: { console.log(y) }
    
            NumberAnimation on y {
                id: anim
                running: false
                duration: 1000
            }
        }
    
        MouseArea {
            onClicked: {
                anim.stop();
                anim.to += 30
                anim.start()
            }
            onDoubleClicked: {
                anim.stop();
                anim.to = icon.y = 0;
            }
    
            anchors.fill: parent
        }
    }
    

    However this is "a lot" of code. In most other cases I'm quite fine with direct bindings to properties.
    Is there a simpler way to achieve this that I missed?
    e.g. Behavior.enabled: newValue > oldValue or in my case even better: icon.setYWithoutBehaviorAnimation(0)?
    (Unfortunately something like 'NumberAnimation.running: to > false' doesn't work either as you cannot bind it to 'to' and 'false')

    Regards, Alex

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kloffy
      wrote on 30 Nov 2015, 05:33 last edited by
      #2

      What I have been using is giving the Behavior an id and:

      onDoubleClicked: { yBehavior.enabled = false; icon.y = 0; yBehavior.enabled = true; }
      

      Feels a bit like a clutch, but it works.

      A 1 Reply Last reply 30 Nov 2015, 08:12
      0
      • K kloffy
        30 Nov 2015, 05:33

        What I have been using is giving the Behavior an id and:

        onDoubleClicked: { yBehavior.enabled = false; icon.y = 0; yBehavior.enabled = true; }
        

        Feels a bit like a clutch, but it works.

        A Offline
        A Offline
        Alexander_Lanin
        wrote on 30 Nov 2015, 08:12 last edited by
        #3

        @kloffy thanks. it's not pretty but way better then what I had.

        1 Reply Last reply
        0

        1/3

        29 Nov 2015, 23:52

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved