2017-10-19 3 views
1

Basé sur le code suivant, j'ai un doute lié à QPropertyAnimation et QParallelAnimationGroup:QPropertyAnimation est automatiquement supprimé lors de l'utilisation de 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. A propos de la QAbstractAnimation::DeleteWhenStopped constante, le Qt documentation dit:

L'animation sera automatiquement supprimé à l'arrêt.

Est-ce que cela signifie que les pointeurs (animation1 et animation2) seront automatiquement supprimés? Ou je dois encore les supprimer manuellement (peut-être en utilisant des signaux et des slots comme ceux qui suivent)?

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

J'utilise Qt 5.3.

+0

Il serait facile à tester - remplacer l'emplacement que vous avez connecté au signal finished de votre groupe, et, d'abord supprimer manuellement le groupe, puis essayez manuellement supprimer une des animations - si votre programme explose, alors le parent standard Qt -> mécanisme de gestion de destruction d'enfant est en jeu –

+0

La réponse est dans le [addAnimation()] (http://doc.qt.io/qt- 5/qanimationgroup.html # addAnimation) docs. "Remarque: Le groupe prend possession de l'animation." – jpnurmi

Répondre

1

Oui, les deux sont détruits.

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(); 
} 

Le résultat est:

Destroyed: "group" 

Destroyed: "animation1" 

Destroyed: "animation2"