3

Based on the following code snippet, I have a doubt related to QPropertyAnimation and QParallelAnimationGroup:

// Create the opacity animation
QPropertyAnimation *animation1 = new QPropertyAnimation(notification, "windowOpacity");
animation1->setDuration(animationDuration);
animation1->setStartValue(startOpacity);
animation1->setEndValue(endOpacity);
animation1->setEasingCurve(QEasingCurve::InBack);

// Create the position animation
QPropertyAnimation *animation2 = new QPropertyAnimation(notification, "pos");
animation2->setDuration(animationDuration);
animation2->setStartValue(startPos);
animation2->setEndValue(endPos);

// Create the animation group
QParallelAnimationGroup *group = new QParallelAnimationGroup;
group->addAnimation(animation1);
group->addAnimation(animation2);
group->start(QAbstractAnimation::DeleteWhenStopped);

connect(group, SIGNAL(finished()), group, SLOT(deleteLater()), Qt::UniqueConnection);
  1. About the QAbstractAnimation::DeleteWhenStopped constant, the Qt documentation says:

The animation will be automatically deleted when stopped.

Does it means that the pointers (animation1 and animation2) will be automatically deleted? Or I still need to 'manually' delete them (maybe using signals and slots like the following ones)?

connect(animation1, SIGNAL(finished()), animation1, SLOT(deleteLater()), Qt::UniqueConnection);
connect(animation2, SIGNAL(finished()), animation2, SLOT(deleteLater()), Qt::UniqueConnection);

I'm using Qt 5.3.

KelvinS
  • 2,870
  • 8
  • 34
  • 67
  • It would be easy to test - replace the slot you have connected to your group's finished signal, and in it, first manually delete the group, and then try manually deleting one of the animations - if your program blows up, then the standard Qt parent -> child destruction management mechanism is in play – Steve Lorimer Oct 19 '17 at 20:44
  • The answer is in the [addAnimation()](http://doc.qt.io/qt-5/qanimationgroup.html#addAnimation) docs. "Note: The group takes ownership of the animation." – jpnurmi Oct 20 '17 at 17:33

1 Answers1

5

Yes, both are destroyed.

animation1->setProperty("nameObj", "animation1");
animation2->setProperty("nameObj", "animation2");
group->setProperty("nameObj", "group");

connect(animation1, SIGNAL(destroyed(QObject*)), this, SLOT(OnAnimationDestroyed(QObject*)));
connect(animation2, SIGNAL(destroyed(QObject*)), this, SLOT(OnAnimationDestroyed(QObject*)));
connect(group, SIGNAL(destroyed(QObject*)), this, SLOT(OnAnimationDestroyed(QObject*)));

group->start(QAbstractAnimation::DeleteWhenStopped);

void MyObj::OnAnimationDestroyed(QObject* obj)
{
    qDebug() << "Destroyed: " << obj->property("nameObj").toString();
}

The result is:

Destroyed:  "group"

Destroyed:  "animation1"

Destroyed:  "animation2"
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rflobao
  • 562
  • 2
  • 8