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->setDuration(300);
ani->setStartValue(QPoint(myX, myY));
ani->setEndValue(QPoint(myX, myY+ 50));
ani->setEasingCurve(QEasingCurve::InCurve);
m_signalMapper->setMapping(ani, ani);
connect(ani, SIGNAL(finished()),m_signalMapper, SLOT(map()));
ani->start();QPropertyAnimation *secondani = new QPropertyAnimation(myObject,"opacity"); secondani->setDuration(800); secondani->setStartValue(1.0); secondani->setEndValue(1.5); secondani->setEasingCurve(QEasingCurve::InCurve); m_signalMapper->setMapping(secondani , secondani ); connect(secondani , SIGNAL(finished()),m_signalMapper, SLOT(map())); secondani->start();
}
void MyClass::DeleteAnimation(QObject *sender)
{
QPropertyAnimation ani = qobject_cast <QPropertyAnimation>(sender);
disconnect(ani, SIGNAL(finished()), this, 0);ani->stop(); 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
-
easiest way is this:
@
anim->start(QAbstractAnimation::DeleteWhenStopped);
@
or connect a the finished signal to the animations deleteLater() slot.