使用QParallelAnimationGroup时,QPropertyAnimation会自动删除吗?基于下面的代码片段
问题描述:
,我有相关的QPropertyAnimation
和QParallelAnimationGroup
一个疑问:使用QParallelAnimationGroup时,QPropertyAnimation会自动删除吗?基于下面的代码片段
// 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);
- 关于
QAbstractAnimation::DeleteWhenStopped
不变,Qt documentation说:
,动画将自动停止时删除。
这是否意味着指针(animation1
和animation2
)会自动删除?或者我仍然需要'手动'删除它们(可能使用像下面这样的信号和插槽)?
connect(animation1, SIGNAL(finished()), animation1, SLOT(deleteLater()), Qt::UniqueConnection);
connect(animation2, SIGNAL(finished()), animation2, SLOT(deleteLater()), Qt::UniqueConnection);
我正在使用Qt 5.3。
答
是的,两者都被破坏。
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();
}
结果是:
Destroyed: "group"
Destroyed: "animation1"
Destroyed: "animation2"
这将是很容易测试 - 更换您已连接到您的团队的最终信号的插槽,并在里面,首先手动删除该组,然后尝试手动删除其中一个动画 - 如果你的程序爆炸了,那么标准的Qt父 - >子销毁管理机制正在播放中 –
答案在[addAnimation()](http://doc.qt.io/qt- 5/qanimationgroup.html#addAnimation)文档。 “注意:该组获得动画的所有权。” – jpnurmi