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. [SOLVED] Object rotating twice on Transition
QtWS25 Last Chance

[SOLVED] Object rotating twice on Transition

Scheduled Pinned Locked Moved QML and Qt Quick
7 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.
  • V Offline
    V Offline
    valorcurse
    wrote on last edited by
    #1

    Hi,

    I've been having problems with animating an object's rotation.
    I have a component which has a State with a rotation change using a random value inside a PropertyChange and whenever I try to use a transition to animate the rotation, it somehow applies two values to the rotation: one before the animation starts and one after. This is annoying because when the animation finishes for the first rotation values, it "jerks" to the value of the second rotation value.

    I recreated the problem "here.":https://gist.github.com/valorcurse/f688baf5e6dcfd714e69

    Any idea what I'm doing wrong?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      I suspect mouse.pressed is causing the problem. Use onClicked handler in MouseArea and there change the state.

      157

      1 Reply Last reply
      0
      • V Offline
        V Offline
        valorcurse
        wrote on last edited by
        #3

        I'm afraid it still sets the rotation value twice.
        I updated "it.":https://gist.github.com/valorcurse/f688baf5e6dcfd714e69

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Remove the when binding

          157

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valorcurse
            wrote on last edited by
            #5

            I believe I did, it's commented out. "I deleted the comment just to be sure.":https://gist.github.com/valorcurse/f688baf5e6dcfd714e69

            Any other idea about what might be the problem?

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              "this":http://qt-project.org/doc/qt-5/qml-qtquick-propertychanges.html#explicit-prop should help you.

              157

              1 Reply Last reply
              0
              • V Offline
                V Offline
                valorcurse
                wrote on last edited by
                #7

                That's exactly it, works perfectly now. Thank you.

                I added the code below in case someone has the same problem:

                @import QtQuick 2.2
                import QtQuick.Window 2.1

                Window {
                visible: true
                width: 360
                height: 360

                MouseArea {
                    id: mouseArea
                    anchors.fill: parent
                    onClicked: {
                        rect.state = rect.state === "clicked" ? "" : "clicked";
                    }
                }
                
                Rectangle {
                    id: rect
                    width: 50
                    height: 50
                    color: "blue"
                    anchors.centerIn: parent
                
                    function getRandom() {
                        var rand = Math.floor(Math.random() * 360) + 1;
                
                        console.log("Random: " + rand);
                
                        return rand;
                    }
                
                    states: [
                        State {
                            name: "clicked"
                
                            PropertyChanges {
                                target: rect
                                explicit: true
                                rotation: getRandom()
                            }
                        }
                    ]
                
                    transitions: Transition {
                        to: "clicked"
                
                        RotationAnimation {
                            target: rect
                            duration: 500
                        }
                    }
                }
                

                }
                @

                1 Reply Last reply
                0

                • Login

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