How to correctly delete QPropertyAnimation?

  • Hello.

    I want to animate an object with to different QPropertyAnimations. The code works fine (animates as wanted) but I´m producing a memoryleak and don´t really know how to handle it. I tried to use QSignalMapper but that doesn´t help.

    First of all my code:
    void MyClass::MyClass()
    m_signalMapper = new QSignalMapper(this);
    connect(m_signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(DeleteAnimation(QObject*)));

    void MyClass::StartAnimation()
    QPropertyAnimation *ani = new QPropertyAnimation(myObject,"pos");
    ani->setStartValue(QPoint(myX, myY));
    ani->setEndValue(QPoint(myX, myY+ 50));
    m_signalMapper->setMapping(ani, ani);
    connect(ani, SIGNAL(finished()),m_signalMapper, SLOT(map()));

        QPropertyAnimation *secondani = new QPropertyAnimation(myObject,"opacity");
        m_signalMapper->setMapping(secondani , secondani );
        connect(secondani , SIGNAL(finished()),m_signalMapper, SLOT(map()));


    void MyClass::DeleteAnimation(QObject *sender)
    QPropertyAnimation ani = qobject_cast <QPropertyAnimation>(sender);
    disconnect(ani, SIGNAL(finished()), this, 0);

        delete ani;
        ani = NULL;


    If the animations are finished "DeleteAnimation()" is called. But the pointer to the object is always the pointer to "secondani". "secondani" is deleted correctly "ani" will never be deleted.

    I tried with start(QPropertyAnimation::DeleteWhenStopped) but that doesn´t work. Connecting the animations directly to "DeleteAnimation()" without using the Signalmapper also doesn´t work. Could someone give me a hint how to do it better?

    To detected the leaks I used VLD.

    Thanks and regards

  • Moderators

    easiest way is this:
    or connect a the finished signal to the animations deleteLater() slot.

Log in to reply

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