<?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 use QTimer in right way ?]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I'm new with Qt and use QTimer but It does not work as my expecting. My issue is:<br />
I have a button. When the button is activated, It will show a QWidget and start countdown timer 8 second to hide the QWidget. However, I want restart countdown timer to 8 second if the button is activated again and countdown timer is less than 8 second.<br />
This is my code:</p>
<pre><code>void trigger(QString message){
              unsigned int timeout = 8000;
              ui-&gt;l_Info-&gt;setText(message);
              ui-&gt;l_Info-&gt;move(0, 0);
              ui-&gt;l_Info-&gt;show();
              QTimer::singleShot(timeout, this, SLOT(hideMessageAtn()));
}

void hideMessageAtn(){
              ui-&gt;l_Info-&gt;hide();
}
</code></pre>
<p dir="auto">Actually, this code does work as my expecting. If I activate button in countdown time, it does not reset coundown timer and still emit hideMessageAtn() in the timeout.<br />
So anyone give me in this case ?<br />
Thank in advance.</p>
]]></description><link>https://forum.qt.io/topic/81861/how-to-use-qtimer-in-right-way</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 14:47:35 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/81861.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 31 Jul 2017 07:25:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to use QTimer in right way ? on Mon, 31 Jul 2017 12:58:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinod-kuntoji">@<bdi>Vinod-Kuntoji</bdi></a> said in <a href="/post/407675">How to use QTimer in right way ?</a>:</p>
<blockquote>
<p dir="auto">...<br />
void myWidget::on_pushButton_clicked()<br />
{<br />
...<br />
timer-&gt;stop();<br />
timer-&gt;setInterval(8000);<br />
timer-&gt;start();<br />
}</p>
</blockquote>
<p dir="auto">This code is a bit reduntant. simply calling timer-&gt;start(8000) will pretty much do the same;</p>
<p dir="auto">Here is a quote from Docs (<a href="https://doc.qt.io/qt-5/qtimer.html#start" target="_blank" rel="noopener noreferrer nofollow ugc">link</a>):</p>
<blockquote>
<p dir="auto">Starts or restarts the timer with the timeout specified in interval.<br />
If the timer is already running, it will be stopped and restarted.<br />
If singleShot is true, the timer will be activated only once.</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/407677</link><guid isPermaLink="true">https://forum.qt.io/post/407677</guid><dc:creator><![CDATA[Darkmalex]]></dc:creator><pubDate>Mon, 31 Jul 2017 12:58:39 GMT</pubDate></item><item><title><![CDATA[Reply to How to use QTimer in right way ? on Mon, 31 Jul 2017 12:40:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hung-tran">@<bdi>Hung-Tran</bdi></a> ,</p>
<p dir="auto">myWidget::myWidget()<br />
{<br />
timer = new QTimer(this);<br />
m_Button = new QPushButton("Click");<br />
widget = new QWidget;<br />
timer-&gt;setSingleShot(true);<br />
connect(m_Button,SIGNAL(clicked()), this,SLOT(on_pushButton_clicked()));<br />
connect(timer,SIGNAL(timeout()), this,SLOT(on_TimerTick()));<br />
}<br />
void myWidget::on_TimerTick()<br />
{<br />
widget -&gt;hide();<br />
}<br />
void myWidget::on_pushButton_clicked()<br />
{<br />
widget -&gt;show();<br />
timer-&gt;stop();<br />
timer-&gt;setInterval(8000);<br />
timer-&gt;start();<br />
}</p>
]]></description><link>https://forum.qt.io/post/407675</link><guid isPermaLink="true">https://forum.qt.io/post/407675</guid><dc:creator><![CDATA[Vinod Kuntoji]]></dc:creator><pubDate>Mon, 31 Jul 2017 12:40:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to use QTimer in right way ? on Mon, 31 Jul 2017 11:43:59 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
AFAIK, there is no easy way to reset a QTimer the way you declared it. Your best bet is to create an object of type QTimer, like this</p>
<pre><code>QTimer *timer = new QTimer(this);
</code></pre>
<p dir="auto">And in your "trigger" function you should do something like this:</p>
<pre><code>timer-&gt;setSingleShot(true);
timer-&gt;start(timeout); //If timer was running, it will restart
</code></pre>
]]></description><link>https://forum.qt.io/post/407671</link><guid isPermaLink="true">https://forum.qt.io/post/407671</guid><dc:creator><![CDATA[Darkmalex]]></dc:creator><pubDate>Mon, 31 Jul 2017 11:43:59 GMT</pubDate></item><item><title><![CDATA[Reply to How to use QTimer in right way ? on Mon, 31 Jul 2017 11:26:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hung-tran">@<bdi>Hung-Tran</bdi></a> ,</p>
<p dir="auto">Once the pushbutton is clicked, show the widget, if the timer is already active, stop it, set the interval  to 8 seconds and then start the timer. Once after the timeout, hide the widget.</p>
]]></description><link>https://forum.qt.io/post/407667</link><guid isPermaLink="true">https://forum.qt.io/post/407667</guid><dc:creator><![CDATA[Vinod Kuntoji]]></dc:creator><pubDate>Mon, 31 Jul 2017 11:26:55 GMT</pubDate></item></channel></rss>