Target deletion when animation is finished



  • Hello Team,

    How do I delete the target object of QProperyAnimation when animation is finished?

    Here is a sample code snippet that I am trying to use.
    @
    label->show();
    QPropertyAnimation *animation = new QPropertyAnimation(this);
    animation->setTargetObject(label);
    animation->setDuration(time);
    animation->setPropertyName("geometry");
    animation->setStartValue(QRect(0, label->height(), label->width(), label->height()));
    animation->setEndValue( QRect(1680, label->height(), label->width(), label->height()));

    connect(animation, signal( finished() ), this, slot( DeleteTarget(label) );

    animation->start(QAbstractAnimation::DeleteWhenStopped);
    @

    As you have noticed I want delete my target object, here label, when the animation is finished. But I realized that I can not pass "label" as a parameter to the slot DeleteTarget().

    So how do I delete the target, the label, after the animation is completed? I don't think I can delete the target after calling animation->start(), as the animation requires it.

    Appreciate your response.

    Rao

    Edit: Added code tags (@) around the code. Please do that yourself next time; Andre



  • From the top of my head.
    @
    connect(animation,SIGNAL(finished()),this,SLOT(deleteTarget()));
    animation->start();

    void deleteTarget()
    {
    QPropertyAnimation animation = qobject_cast<QPropertyAnimation>(sender());
    if(animation)
    {
    animation->targetObject()->deleteLater();
    animation->deleteLater();
    }
    }
    @

    or
    @
    connect(animation,SIGNAL(finished()),label,SLOT(deleteLater()));
    @



  • Thanks for the response. If I call

    @
    connect(animation,SIGNAL(finished()) ,label, SLOT(deleteLater()));
    @

    When is the label deleted?

    1. Is it when the finished signal is emitted or
    2. later, when the program exits?

    Thanks



  • From the documentation. Try having a look at it sometimes. :-P

    @
    void QObject::deleteLater () [slot]
    @

    Schedules this object for deletion.
    The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.