Stop QML animation from c++



  • Hi,

    I have made a custom component subclassing QDeclarativeItem.
    In QML I have for example:

    @
    MyItem {
    Behavior on x { NumberAnimation { duration: 1500 } }
    [...]
    }@

    I would like to stop all the animations without using some binding QML code inside MyItem just because it will be a plugin and I don't want the user to take care of this.
    I seen that Behaviour isn't a QDeclarativeItem , it is a QAbstractAnimation if I remember well. So if I parse all childrens of MyItem (in c++), it is not listed and I cant call the stop() function. But maybe I am wrong.
    Any idea ?

    thanks in advance and happy new year :)
    Marco


  • Moderators

    Maybe adding id to this behaviour would improve things? If no, you can try writing this a bit differently:
    @
    MyItem {
    NumberAnimation on x { duration: 1500 }
    [...]
    }
    @
    That might work.

    Both are just guesses, though. In my code, I handle animations with JS.



  • thank sierdzio for the replay.

    My wishes is to call "complete()":http://developer.qt.nokia.com/doc/qt-4.8/qml-animation.html#complete-method for all animations inside c++. In your code example, I found this:

    @QObject *item;
    foreach (item, children() )
    qDebug() << item->metaObject()->className(); @

    the output is "QDeclarativeNumberAnimation". This is a class derived from QDeclarativeAbstractAnimation.
    I would like for example to call:

    @qobject_cast<QDeclarativeAbstractAnimation*>(item)->complete();@

    but QDeclarativeAbstractAnimation is private and I think this could not be achieved without some binds in QML.


  • Moderators

    What about:
    @
    // QML
    MyItem {
    NumberAnimation on x { alwaysRunToEnd: true; duration: 1500 }
    [...]
    }

    // C++
    item.setProperty("running", QVariant(false));
    @

    Or, try casting to QDeclarativeNumberAnimation - if it exists. I am sure there was some example on how to access animations from C++ in QML documentation, but I can't find it now.


  • Moderators



  • QDeclarativeNumberAnimation is private also :( , but you gave me some useful reminds that make me try to continue. The "alwaysRunToEnd" makes the animation to terminate but it is animated till it ends, but maybe I can bypass this with something other.

    For your consideration I made this "video":http://www.youtube.com/watch?v=-y8boYYCsc4 : at 48" , when I press "menu 3" and then a sub menu, I would like to terminate all animations in the first one otherwise unwanted behavior should happens if sub menu is open before animations end (ops, maybe I been unclear with this sentence, but video explains :) ).

    thanks again


  • Moderators

    I'll think about it later, currently I'm deep into an algorithm I'm writing any my brain is steaming :)

    Just to clarify, though: have you tried using item->setProperty() or item->invokeMethod() without casting? I think it should work, but - as I've mentioned - my cognitive powers are diminished at the moment :)



  • yes setProperty and invokeMethod are working.
    thanks again


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.