下面是QPropertyAnimation的创建代码:
void CustomGraphicsScene::addAnimation(AnimatedPixmapItem* item)
{
auto propertyAnimation = new QPropertyAnimation { item, "SpriteFrame" };
connect(propertyAnimation, &QPropertyAnimation::destroyed, this, &CustomGraphicsScene::deleteAnimation);
propertyAnimation->setDuration(1000);
propertyAnimation->setStartValue(0);
propertyAnimation->setEndValue(10);
propertyAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}这是我尝试使用QPropertyAnimation的位置:
void CustomGraphicsScene::deleteAnimation()
{
auto propertyAnimation = qobject_cast<QPropertyAnimation*>(sender());
if (propertyAnimation) { // Always false
// Some code
}
}发布于 2021-08-26 22:20:04
qobject_cast不仅执行强制转换,还使用QMetaObject验证QObject是否仍然有效,例如,如果在构建对象时打印propertyAnimation->metaObject()->className();,它将返回QPropertyAnimation,但在与已销毁的sender()->metaObject()->className()相关联的插槽中,它将返回QObject。
我的建议是使用finished,更改删除策略并在插槽中删除它。
void CustomGraphicsScene::addAnimation(AnimatedPixmapItem* item)
{
auto propertyAnimation = new QPropertyAnimation { item, "SpriteFrame" };
connect(propertyAnimation, &QPropertyAnimation::finished, this, &CustomGraphicsScene::finishAnimation);
propertyAnimation->setDuration(1000);
propertyAnimation->setStartValue(0);
propertyAnimation->setEndValue(10);
propertyAnimation->start(QAbstractAnimation::KeepWhenStopped);
}
void CustomGraphicsScene::finishAnimation()
{
auto propertyAnimation = qobject_cast<QPropertyAnimation*>(sender());
if (propertyAnimation) { // Always false
// Some code
propertyAnimation->deleteLater();
}
}https://stackoverflow.com/questions/68934546
复制相似问题