<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to correctly delete QPropertyAnimation?]]></title><description><![CDATA[<p dir="auto">Hello.</p>
<p dir="auto">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.</p>
<p dir="auto">First of all my code:<br />
@<br />
void MyClass::MyClass()<br />
{<br />
m_signalMapper = new QSignalMapper(this);<br />
connect(m_signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(DeleteAnimation(QObject*)));<br />
}</p>
<p dir="auto">void MyClass::StartAnimation()<br />
{<br />
QPropertyAnimation *ani = new QPropertyAnimation(myObject,"pos");<br />
ani-&gt;setDuration(300);<br />
ani-&gt;setStartValue(QPoint(myX, myY));<br />
ani-&gt;setEndValue(QPoint(myX, myY+ 50));<br />
ani-&gt;setEasingCurve(QEasingCurve::InCurve);<br />
m_signalMapper-&gt;setMapping(ani, ani);<br />
connect(ani, SIGNAL(finished()),m_signalMapper, SLOT(map()));<br />
ani-&gt;start();</p>
<pre><code>    QPropertyAnimation *secondani = new QPropertyAnimation(myObject,"opacity");
    secondani-&gt;setDuration(800);
    secondani-&gt;setStartValue(1.0);
    secondani-&gt;setEndValue(1.5);
    secondani-&gt;setEasingCurve(QEasingCurve::InCurve);
    m_signalMapper-&gt;setMapping(secondani , secondani );
    connect(secondani , SIGNAL(finished()),m_signalMapper, SLOT(map()));
    secondani-&gt;start();
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void MyClass::DeleteAnimation(QObject *sender)<br />
{<br />
QPropertyAnimation <em>ani = qobject_cast &lt;QPropertyAnimation</em>&gt;(sender);<br />
disconnect(ani, SIGNAL(finished()), this, 0);</p>
<pre><code>    ani-&gt;stop();
    delete ani;
    ani = NULL;
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">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.</p>
<p dir="auto">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?</p>
<p dir="auto">To detected the leaks I used VLD.</p>
<p dir="auto">Thanks and regards</p>
]]></description><link>https://forum.qt.io/topic/30458/how-to-correctly-delete-qpropertyanimation</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Apr 2026 05:39:59 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/30458.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 08 Aug 2013 09:20:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to correctly delete QPropertyAnimation? on Thu, 08 Aug 2013 09:26:52 GMT]]></title><description><![CDATA[<p dir="auto">easiest way is this:<br />
@<br />
anim-&gt;start(QAbstractAnimation::DeleteWhenStopped);<br />
@<br />
or connect a the finished signal to the animations deleteLater() slot.</p>
]]></description><link>https://forum.qt.io/post/190121</link><guid isPermaLink="true">https://forum.qt.io/post/190121</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 08 Aug 2013 09:26:52 GMT</pubDate></item></channel></rss>