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] position change from custom component and animation in QML
Forum Updated to NodeBB v4.3 + New Features

[solved] position change from custom component and animation in QML

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 6.6k Views 1 Watching
  • 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 Offline
    D Offline
    deimos
    wrote on last edited by
    #1

    Hi,

    I implemented a custom QDeclarativeItem component. The problem is that if I change its position from c++ the code in QML to animate it doesn't work . If I change it in QML the animation works.
    @
    MyItem {
    id: myItem
    onXChanged: console.log("XCHANGED",x);

         Behavior on x { NumberAnimation { duration: 1000 } }
    

    }

    MouseArea {
    anchors.fill: myItem
    onClicked: myItem.x+=100
    }
    @

    For example with this code, if I click on the item animation starts, but if I change position in c++ code, onXChanged is catched but the animation doesn't start.
    I suppose that animations get called only when the property is changed by QML ? Is there a workaround or maybe I miss something ?
    As a workaround I think I could be make a custom x variable and when changed from c++ get that change from QML and then set new x value from there, for example

    @onMyxChanged: x = my_x@

    but should not be a definitive solution because this is a component plugin and the position change should be transparent to user

    thanks for your time

    1 Reply Last reply
    0
    • D Offline
      D Offline
      deimos
      wrote on last edited by
      #2

      I found that doing this

      @setProperty ("x", x); @

      in the QDeclarativeItem subclass, makes the qml animation to work

      1 Reply Last reply
      0
      • M Offline
        M Offline
        muthu005
        wrote on last edited by
        #3

        me too facing the similar problem. but i did not understand how you solved it. can you please elaborate in detail.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          deimos
          wrote on last edited by
          #4

          by calling QDeclarativeItem::setX() the position of the component is changed, but the signal onXChanged isn't raised and the animation can't start. If you call setProperty it is, because it is like you change it in QML source.
          remember to set

          @setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);@

          and not to call the setProperty in your constructor because the item isn't yet "completed".
          I didn't understand this behavior well, but maybe there is another better solution ?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            muthu005
            wrote on last edited by
            #5

            Thank you deimos :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              meolic
              wrote on last edited by
              #6

              I have slightly extended problem. If I made two changes on x (call setProperty twice) only the second one will be animated. I do not know if the element is started to move to the first destination and then almost imediately changes to the second destination, or something else hapened, but I do not get the result which I want: to see both animations one after other. I have asked this question already in Q&A but I did not get the answer, yet. Maybe you have some solution?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deimos
                wrote on last edited by
                #7

                Yes, you right. The animation of the x property is its current value. So if you change it before animation ends, the last value is used in the animation.
                A solution that come to my mind is to use for example 2 properties in your custom item and when you change one of those with setProperty, a SequentialAnimation could start:

                @MyItem {
                id: myItem
                property real x1
                property real x2

                 onX2Changed: anim.running = true
                
                 SequentialAnimation {
                      id: anim
                      running: false
                      NumberAnimation { target: myItem; property: "x"; to: x1; duration: 1000 }
                      NumberAnimation { target: myItem; property: "x"; to: x2; duration: 1000 }
                 }
                

                }@

                Hope this help, let me know if it works

                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