<?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[Qtimer - Problem to destroy object]]></title><description><![CDATA[<p dir="auto">Hi All,<br />
Sorry for this stupid question, but i'm unable to understand this behavior.  (I'm a newbie with c++ and Qt)<br />
This is my code :</p>
<pre><code>void MainWindow::on_ResistenzeOn_clicked()
{

    
    countDown = new QTimer(this);
    //int val = ui-&gt;lcdSetTimer-&gt;value();
    connect(countDown,SIGNAL(timeout()),this,SLOT(countDownSlot()));
    countDown-&gt;start(1000);
    gpioWrite(OUT_RES, 1);
    ui-&gt;minutiPiu-&gt;setEnabled(false);
    ui-&gt;minutiMeno-&gt;setEnabled(false);
    ui-&gt;labelResistence-&gt;setPixmap(MainWindow::fireScaled);


}


void MainWindow::on_ResistenzeOFF_clicked()
{

    ui-&gt;minutiPiu-&gt;setEnabled(true);
    ui-&gt;minutiMeno-&gt;setEnabled(true);
    countDown-&gt;stop();
    delete countDown;
    gpioWrite(OUT_RES, 0);
    ui-&gt;labelResistence-&gt;setPixmap(MainWindow::coldScaled);

}

void MainWindow::countDownSlot() {
    static int setTime = 60;
    if((--setTime) &gt; 0)
        ui-&gt;lcdGetResTimer-&gt;display(setTime);
    else {
        qDebug() &lt;&lt; "sono a zero";

    }

}

</code></pre>
<p dir="auto">When i click on ResistenzeOn() the countDown start and the lcdGetResTimer display the countdown .. 59 .. 58 .. 57 ... and so on..<br />
But when i click ResistenzeOFF() the countDown stop, but if click again ResistenzeOn the LcdGetResTimer don't display every number but the number - 2 e.g 55 .. 53 .. 51 ..49 an so on..<br />
I miss something..<br />
Someone can help me?<br />
Thanks,<br />
Giovanni.</p>
]]></description><link>https://forum.qt.io/topic/63381/qtimer-problem-to-destroy-object</link><generator>RSS for Node</generator><lastBuildDate>Thu, 14 May 2026 23:43:10 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/63381.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Jan 2016 17:37:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qtimer - Problem to destroy object on Tue, 26 Jan 2016 22:05:28 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You can also make MainWindow the parent of the timer so there's no need for the delete call in MainWindow's destructor.</p>
]]></description><link>https://forum.qt.io/post/309767</link><guid isPermaLink="true">https://forum.qt.io/post/309767</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 26 Jan 2016 22:05:28 GMT</pubDate></item><item><title><![CDATA[Reply to Qtimer - Problem to destroy object on Tue, 26 Jan 2016 21:42:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> You're right with everything you said. Of course this is all messed up and needs to be refactored like you suggested. I just thought you had spotted another, more subtile bug that I haven't noticed. :-)</p>
]]></description><link>https://forum.qt.io/post/309757</link><guid isPermaLink="true">https://forum.qt.io/post/309757</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 26 Jan 2016 21:42:48 GMT</pubDate></item><item><title><![CDATA[Reply to Qtimer - Problem to destroy object on Tue, 26 Jan 2016 21:29:13 GMT]]></title><description><![CDATA[<p dir="auto">@Wieland<br />
Why should what be a problem? If you're talking about the static variable, it shouldn't. However it's not reinitialized in the beginning of each counting cycle, which I inferred was the desired behavior from the problem description, namely: <em>"but if click again ResistenzeOn the LcdGetResTimer don't display every number but the number - 2 e.g 55 .. 53 .. 51 ..49 an so on.."</em>. In my opinion this timer is prime candidate for being created as member of the main window, instead of being re-created on every click, and then there's no problem with hanging objects (which you correctly noted in your comment). Something like this:</p>
<pre><code>class MainWindow : public QMainWindow
{
     // ...
     QTimer countDown;
}
</code></pre>
<p dir="auto">or, if <a class="plugin-mentions-user plugin-mentions-a" href="/user/ciclonite">@<bdi>ciclonite</bdi></a> still wishes to allocate it in the heap, at least it's better to do it in the constructor:</p>
<pre><code>class MainWindow : public QMainWindow
{
    // ...
    QTimer * countDown;
}

MainWindow::MainWindow()
    : QMainWindow(), countDown(new QTimer)
{
}

MainWindow::~MainWindow()
{
    delete countDown;
}
</code></pre>
<p dir="auto">Kind regards.</p>
]]></description><link>https://forum.qt.io/post/309753</link><guid isPermaLink="true">https://forum.qt.io/post/309753</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Tue, 26 Jan 2016 21:29:13 GMT</pubDate></item><item><title><![CDATA[Reply to Qtimer - Problem to destroy object on Tue, 26 Jan 2016 19:01:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> Why should this be a problem in this case?</p>
]]></description><link>https://forum.qt.io/post/309722</link><guid isPermaLink="true">https://forum.qt.io/post/309722</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 26 Jan 2016 19:01:26 GMT</pubDate></item><item><title><![CDATA[Reply to Qtimer - Problem to destroy object on Tue, 26 Jan 2016 19:00:50 GMT]]></title><description><![CDATA[<p dir="auto">In <code>on_ResistenzeOn_clicked</code> you don't check if you already have a QTimer object. If you already have one (because you called <code>on_ResistenzeOn_clicked</code> before but did not call <code>on_ResistenzeOff_clicked</code>) then you'll have 2 running timers. Next time you'll have 3. And because you only store a pointer to one timer (always the latest one you created) you can no longer stop the ones you created previously.</p>
]]></description><link>https://forum.qt.io/post/309721</link><guid isPermaLink="true">https://forum.qt.io/post/309721</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 26 Jan 2016 19:00:50 GMT</pubDate></item><item><title><![CDATA[Reply to Qtimer - Problem to destroy object on Tue, 26 Jan 2016 17:43:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ciclonite">@<bdi>ciclonite</bdi></a></p>
<pre><code>static int setTime = 60;
</code></pre>
<p dir="auto">This line is why it's happening like this. Put the <code>setTime</code> variable in your class as a member variable and set it to its initial value every time you start the timer (in the <code>on_ResistenzeOn_clicked</code> slot).</p>
<p dir="auto">Kind regards.</p>
]]></description><link>https://forum.qt.io/post/309710</link><guid isPermaLink="true">https://forum.qt.io/post/309710</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Tue, 26 Jan 2016 17:43:40 GMT</pubDate></item></channel></rss>