<?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[If condition to stay for a fixed time]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I've a basic question. In the following code I'm switching the appearance (using <code>StyleSheetOn</code> and <code>StyleSheetOff</code> ) of a radio button based on IF condition.</p>
<p dir="auto">How do I make the <code>StyleSheetOn</code> appear for a fixed period of time , say 1 sec?</p>
<pre><code>if(a &gt; 0.5)
{
    ui-&gt;rdo_btn-&gt;show();
    ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOn);
}
else
{
    ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOff);
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/116876/if-condition-to-stay-for-a-fixed-time</link><generator>RSS for Node</generator><lastBuildDate>Fri, 07 Aug 2026 12:14:00 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/116876.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 10 Jul 2020 08:16:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to If condition to stay for a fixed time on Mon, 13 Jul 2020 06:58:59 GMT]]></title><description><![CDATA[<p dir="auto">With the code that <a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> provided the radio button will stay on for <strong>at most</strong> 1 second. If <code>a</code> falls below 0.5 earlier than that it will turn the button off immediately. If you want something like <strong>exactly</strong> 1 second I would suggest introducing a boolean member variable which will be reset by the timer.</p>
<p dir="auto">Furthermore, I suggest that you use a single stylesheet for the two states specifying the different style for different toggle states. The right one will be picked according to the checked state of the radio button.</p>
<p dir="auto">Together, the code would look something like this:</p>
<pre><code>if(!keep_on) // don't change anything if the timer is still running (neither extend the "on" period nor toggle it off)
{
    if(a &gt; 0.5)
    {
        ui-&gt;rd_btn-&gt;show();
        ui-&gt;rd_btn-&gt;setChecked(true);

        keep_on = true;
        QTimer::singleShot(1000, this, [this]() { keep_on = false; ui-&gt;rdo_btn-&gt;setChecked(false); });
    }
    else
    {
        ui-&gt;rd_btn-&gt;setChecked(false);
    }
}
</code></pre>
<p dir="auto">You would also need to set the stylesheet once in the constructor, for example.</p>
<p dir="auto">Depending on what you really want, this might not be the right solution. If the on-state should be extended by another <code>a &gt; 0.5</code> while the timer is still running, you would need to adapt the code accordingly. If you want <code>a &lt;= 0.5</code> to immediatly switch to the off-state this needs changes as well.</p>
]]></description><link>https://forum.qt.io/post/606394</link><guid isPermaLink="true">https://forum.qt.io/post/606394</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 13 Jul 2020 06:58:59 GMT</pubDate></item><item><title><![CDATA[Reply to If condition to stay for a fixed time on Fri, 10 Jul 2020 09:04:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/russjohn834">@<bdi>russjohn834</bdi></a> said in <a href="/post/606001">If condition to stay for a fixed time</a>:</p>
<blockquote>
<p dir="auto">Could you please comment what went wrong here?</p>
</blockquote>
<p dir="auto">The code here looks good. So the question is rather: how often is it called? Do you call this method many times per second, does <code>a</code> change often?</p>
]]></description><link>https://forum.qt.io/post/606002</link><guid isPermaLink="true">https://forum.qt.io/post/606002</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 10 Jul 2020 09:04:18 GMT</pubDate></item><item><title><![CDATA[Reply to If condition to stay for a fixed time on Fri, 10 Jul 2020 09:02:29 GMT]]></title><description><![CDATA[<p dir="auto">If I do this:</p>
<pre><code>if(a &gt; 0.5)
{
    ui-&gt;rdo_btn-&gt;show();
    ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOn);

    QTimer::singleShot(1000, this, [this, StyleSheetOff]() { ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOff); });
}
else
{
    ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOff);
}
</code></pre>
<p dir="auto">Apparently it behaves as before (it momentarily shows the <code>StyleSheetOn</code> and shows <code>StyleSheetOff</code> ).</p>
<p dir="auto">What I'm trying to do is , if the condition met (<code>if(a &gt; 0.5)</code>) , radio button (<code>rdo_btn</code>) should show the  <code>StyleSheetOn</code> for <strong>1 sec</strong> and then <code>StyleSheetOff</code>.</p>
<p dir="auto">Could you please comment what went wrong here?</p>
]]></description><link>https://forum.qt.io/post/606001</link><guid isPermaLink="true">https://forum.qt.io/post/606001</guid><dc:creator><![CDATA[russjohn834]]></dc:creator><pubDate>Fri, 10 Jul 2020 09:02:29 GMT</pubDate></item><item><title><![CDATA[Reply to If condition to stay for a fixed time on Fri, 10 Jul 2020 08:45:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/russjohn834">@<bdi>russjohn834</bdi></a></p>
<pre><code>QTimer::singleShot(1000, this, [this, StyleSheetOff]() { ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOff); });
</code></pre>
]]></description><link>https://forum.qt.io/post/605996</link><guid isPermaLink="true">https://forum.qt.io/post/605996</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 10 Jul 2020 08:45:59 GMT</pubDate></item><item><title><![CDATA[Reply to If condition to stay for a fixed time on Fri, 10 Jul 2020 08:44:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> Hi, I tried as you suggested, I get an error saying:</p>
<pre><code>mainwindow.cpp:348:87: error: variable 'StyleSheetOff' cannot be implicitly captured in a lambda with no capture-default specified
mainwindow.cpp:319:13: note: 'StyleSheetOff' declared here
mainwindow.cpp:348:40: note: lambda expression begins here
</code></pre>
]]></description><link>https://forum.qt.io/post/605994</link><guid isPermaLink="true">https://forum.qt.io/post/605994</guid><dc:creator><![CDATA[russjohn834]]></dc:creator><pubDate>Fri, 10 Jul 2020 08:44:22 GMT</pubDate></item><item><title><![CDATA[Reply to If condition to stay for a fixed time on Fri, 10 Jul 2020 08:19:17 GMT]]></title><description><![CDATA[<p dir="auto">Use a QTimer, so example like this:</p>
<pre><code>if(a &gt; 0.5)
{
    ui-&gt;rdo_btn-&gt;show();
    ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOn);

    QTimer::singleShot(1000, this, [this]() { ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOff); });
}
else
{
    ui-&gt;rdo_btn-&gt;setStyleSheet(StyleSheetOff);
}
</code></pre>
]]></description><link>https://forum.qt.io/post/605986</link><guid isPermaLink="true">https://forum.qt.io/post/605986</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 10 Jul 2020 08:19:17 GMT</pubDate></item></channel></rss>