<?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[Destroying&#x2F;disconnecting a QTimer initialized in another function]]></title><description><![CDATA[<p dir="auto">Hi. I have a QTimer on the heap that is created every time a certain function is called. I would like to make sure I only have one of these QTimers by destroying the previous QTimer every time at the start of that certain function being called. This is how the Timer is made:</p>
<pre><code>void ApplicationSettings::on_combo_changeBar_activated(int index){
    QTimer *timer = new QTimer(this);

    int seconds;
    switch (index){     //Written for redundancy. Future values are probably not gonna be in 2s.
    case 0:
        seconds = 1;
        break;
    case 1:
        seconds = 2;
        break;
    case 2:
        seconds = 4;
        break;
    case 3:
        seconds = 6;
        break;
    case 4:
        seconds = 8;
        break;
    default:
        seconds = 2;
    }
    qDebug() &lt;&lt; seconds;
    ui-&gt;progressBar-&gt;setValue(0);

    connect(timer, &amp;QTimer::timeout, this, [=](){this-&gt;otherFunction();});
    timer-&gt;start(seconds*10);
}
</code></pre>
<p dir="auto">How can I destroy this QTimer the next time this function is called, such that there will only be one at all times? I wasn't able to find any documentation in the <a href="https://doc.qt.io/qt-6/qtimer.html" target="_blank" rel="noopener noreferrer nofollow ugc">QTimer documentation.</a></p>
]]></description><link>https://forum.qt.io/topic/140338/destroying-disconnecting-a-qtimer-initialized-in-another-function</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 21:13:20 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/140338.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 Oct 2022 13:45:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Destroying&#x2F;disconnecting a QTimer initialized in another function on Wed, 26 Oct 2022 18:23:15 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">In addition to my fellows, are you looking to just restart that timer ?<br />
If so, then <a class="plugin-mentions-user plugin-mentions-a" href="/user/mchinand">@<bdi>mchinand</bdi></a> solution is the most simple and effective beside having your QTimer as object member rather than pointer.</p>
]]></description><link>https://forum.qt.io/post/734175</link><guid isPermaLink="true">https://forum.qt.io/post/734175</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 26 Oct 2022 18:23:15 GMT</pubDate></item><item><title><![CDATA[Reply to Destroying&#x2F;disconnecting a QTimer initialized in another function on Wed, 26 Oct 2022 17:11:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dummie1138">@<bdi>Dummie1138</bdi></a> said in <a href="/post/734157">Destroying/disconnecting a QTimer initialized in another function</a>:</p>
<blockquote>
<p dir="auto">whether there is a way to simply destroy the timer when the function is redeclared.</p>
</blockquote>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> says, we do not know what "redeclared" means here.</p>
<p dir="auto">Assuming you mean when this function is <em>called</em> again.  To destroy (or reset) the timer in the function you will need a variable which refers to the timer.  You cannot have a function local variable retain its value between calls (don't use <code>static</code> here).  So you will need to store that reference variable outside the function somewhere.  That is why it would need to be a class variable.</p>
]]></description><link>https://forum.qt.io/post/734171</link><guid isPermaLink="true">https://forum.qt.io/post/734171</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 26 Oct 2022 17:11:05 GMT</pubDate></item><item><title><![CDATA[Reply to Destroying&#x2F;disconnecting a QTimer initialized in another function on Wed, 26 Oct 2022 14:45:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dummie1138">@<bdi>Dummie1138</bdi></a> said in <a href="/post/734157">Destroying/disconnecting a QTimer initialized in another function</a>:</p>
<blockquote>
<p dir="auto">when the function is redeclared.</p>
</blockquote>
<p dir="auto">What does this mean?</p>
]]></description><link>https://forum.qt.io/post/734158</link><guid isPermaLink="true">https://forum.qt.io/post/734158</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 26 Oct 2022 14:45:12 GMT</pubDate></item><item><title><![CDATA[Reply to Destroying&#x2F;disconnecting a QTimer initialized in another function on Wed, 26 Oct 2022 14:05:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mchinand">@<bdi>mchinand</bdi></a> Thank you very much for the response. I am aware that I can declare my timer as part of my class, but I was wondering whether there is a way to simply destroy the timer when the function is redeclared.</p>
<p dir="auto">This function is meant to be declared every 20-80 mills, btw.</p>
]]></description><link>https://forum.qt.io/post/734157</link><guid isPermaLink="true">https://forum.qt.io/post/734157</guid><dc:creator><![CDATA[Dummie1138]]></dc:creator><pubDate>Wed, 26 Oct 2022 14:05:52 GMT</pubDate></item><item><title><![CDATA[Reply to Destroying&#x2F;disconnecting a QTimer initialized in another function on Wed, 26 Oct 2022 14:05:21 GMT]]></title><description><![CDATA[<p dir="auto">Also, note that the <a href="https://doc.qt.io/qt-6/qtimer.html#start" target="_blank" rel="noopener noreferrer nofollow ugc">start</a> method parameter is in milliseconds and there are 1000 milliseconds in a second.</p>
]]></description><link>https://forum.qt.io/post/734156</link><guid isPermaLink="true">https://forum.qt.io/post/734156</guid><dc:creator><![CDATA[mchinand]]></dc:creator><pubDate>Wed, 26 Oct 2022 14:05:21 GMT</pubDate></item><item><title><![CDATA[Reply to Destroying&#x2F;disconnecting a QTimer initialized in another function on Wed, 26 Oct 2022 14:00:38 GMT]]></title><description><![CDATA[<p dir="auto">Have your timer object be a private member of your class and then do:</p>
<pre><code>void ApplicationSettings::on_combo_changeBar_activated(int index){
   if (timer == nullptr){// or instantiate it in the constructor
      timer = new QTimer();
      connect(timer, &amp;QTimer::timeout, this, [=](){this-&gt;otherFunction();});
   }
   if (timer-&gt;isActive()){
      timer-&gt;stop();
   }
   .
   .
   .
   timer-&gt;start(seconds*10);
}</code></pre>
]]></description><link>https://forum.qt.io/post/734154</link><guid isPermaLink="true">https://forum.qt.io/post/734154</guid><dc:creator><![CDATA[mchinand]]></dc:creator><pubDate>Wed, 26 Oct 2022 14:00:38 GMT</pubDate></item></channel></rss>