Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to correctly delete QPropertyAnimation?
Qt 6.12 LTS is out! See what's new in the release blog

How to correctly delete QPropertyAnimation?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello.

    I want to animate an object with to different QPropertyAnimations. The code works fine (animates as wanted) but I´m producing a memoryleak and don´t really know how to handle it. I tried to use QSignalMapper but that doesn´t help.

    First of all my code:
    @
    void MyClass::MyClass()
    {
    m_signalMapper = new QSignalMapper(this);
    connect(m_signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(DeleteAnimation(QObject*)));
    }

    void MyClass::StartAnimation()
    {
    QPropertyAnimation *ani = new QPropertyAnimation(myObject,"pos");
    ani->setDuration(300);
    ani->setStartValue(QPoint(myX, myY));
    ani->setEndValue(QPoint(myX, myY+ 50));
    ani->setEasingCurve(QEasingCurve::InCurve);
    m_signalMapper->setMapping(ani, ani);
    connect(ani, SIGNAL(finished()),m_signalMapper, SLOT(map()));
    ani->start();

        QPropertyAnimation *secondani = new QPropertyAnimation(myObject,"opacity");
        secondani->setDuration(800);
        secondani->setStartValue(1.0);
        secondani->setEndValue(1.5);
        secondani->setEasingCurve(QEasingCurve::InCurve);
        m_signalMapper->setMapping(secondani , secondani );
        connect(secondani , SIGNAL(finished()),m_signalMapper, SLOT(map()));
        secondani->start();
    

    }

    void MyClass::DeleteAnimation(QObject *sender)
    {
    QPropertyAnimation ani = qobject_cast <QPropertyAnimation>(sender);
    disconnect(ani, SIGNAL(finished()), this, 0);

        ani->stop();
        delete ani;
        ani = NULL;
    

    }
    @

    If the animations are finished "DeleteAnimation()" is called. But the pointer to the object is always the pointer to "secondani". "secondani" is deleted correctly "ani" will never be deleted.

    I tried with start(QPropertyAnimation::DeleteWhenStopped) but that doesn´t work. Connecting the animations directly to "DeleteAnimation()" without using the Signalmapper also doesn´t work. Could someone give me a hint how to do it better?

    To detected the leaks I used VLD.

    Thanks and regards

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      easiest way is this:
      @
      anim->start(QAbstractAnimation::DeleteWhenStopped);
      @
      or connect a the finished signal to the animations deleteLater() slot.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved