<?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[Check if widget is pushbutton]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I have a widget <code>QWidget*</code> and I need to check is that widget is a <code>QPushButton*</code> (or <code>QFrame*</code> or any inherited from <code>QWidget*</code>). How to do that? The code:</p>
<pre><code>if (static_cast&lt;QPushButton*&gt;(widget)){
    ....
}
</code></pre>
<p dir="auto">doesn't seem to work...</p>
]]></description><link>https://forum.qt.io/topic/117141/check-if-widget-is-pushbutton</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 18:56:29 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/117141.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 18 Jul 2020 13:47:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:44:47 GMT]]></title><description><![CDATA[<p dir="auto">Yes it's better than string comparison, but it's 2 lines :-)</p>
<pre><code>auto pushButton = qobject_cast&lt;QPushButton*&gt;(widget);
if (nullptr != pushButton)
</code></pre>
]]></description><link>https://forum.qt.io/post/607584</link><guid isPermaLink="true">https://forum.qt.io/post/607584</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:44:47 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Mon, 20 Jul 2020 08:38:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a> said in <a href="/post/607780">Check if widget is pushbutton</a>:</p>
<blockquote>
<p dir="auto">Has anyone any idea why the version suggested by @LeLev would work?</p>
</blockquote>
<p dir="auto">I've been using this feature for many years, it's definitely NOT a C++17 feature.<br />
As I look into <a href="https://en.cppreference.com/w/cpp/language/if" target="_blank" rel="noopener noreferrer nofollow ugc">https://en.cppreference.com/w/cpp/language/if</a> , it is called</p>
<blockquote>
<p dir="auto">declaration of a single non-array variable with a brace-or-equals initializer</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/607781</link><guid isPermaLink="true">https://forum.qt.io/post/607781</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Mon, 20 Jul 2020 08:38:26 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Mon, 20 Jul 2020 08:29:34 GMT]]></title><description><![CDATA[<p dir="auto">@LeLev said in <a href="/post/607585">Check if widget is pushbutton</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a> can't you do?</p>
<pre><code>if(auto pushButton = qobject_cast&lt;QPushButton*&gt;(pushButton ))
</code></pre>
</blockquote>
<p dir="auto">It looks like several C++ features got mixed up in this post. The original question wasn't too far off:</p>
<pre><code>if (static_cast&lt;QPushButton*&gt;(widget))
</code></pre>
<p dir="auto">The only problem here is that the wrong type of cast was used. <code>static_cast&lt;&gt;()</code> means: "I know what I am doing.", i.e. assume that the cast is always allowed. The correct C++ feature is <code>dynamic_cast&lt;&gt;()</code>. So, a C++-only solution would look like this:</p>
<pre><code>if(dynamic_cast&lt;QPushButton*&gt;(widget))
</code></pre>
<p dir="auto">In the context of Qt it was correctly pointed out that <code>qobject_cast&lt;&gt;()</code> is preferrable over <code>dynamic_cast&lt;&gt;()</code> (as it might be faster and does not need RTTI). A normal replacement of the cast is the easiest solution here:</p>
<pre><code>if(qobject_cast&lt;QPushButton*&gt;(widget))
</code></pre>
<p dir="auto">I am not entirely sure what C++ feature would enable the variant suggested by @LeLev :</p>
<pre><code>if(auto pushButton = qobject_cast&lt;QPushButton*&gt;(pushButton ))
</code></pre>
<p dir="auto">As far as I understand variable declarations are not allowed in if statements. C++17 introduced such a feature, however it has the declaration and the condition separated:</p>
<pre><code>if(auto pushButton = qobject_cast&lt;QPushButton*&gt;(pushButton ); pushButton)  // this works since C++17
</code></pre>
<p dir="auto">Has anyone any idea why the version suggested by @LeLev would work?</p>
]]></description><link>https://forum.qt.io/post/607780</link><guid isPermaLink="true">https://forum.qt.io/post/607780</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Mon, 20 Jul 2020 08:29:34 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sun, 19 Jul 2020 17:59:50 GMT]]></title><description><![CDATA[<p dir="auto">Even it works you should follow the official documentation: <a href="https://qwt.sourceforge.io/qwtinstall.html#COMPILEANDLINKAPP" target="_blank" rel="noopener noreferrer nofollow ugc">https://qwt.sourceforge.io/qwtinstall.html#COMPILEANDLINKAPP</a></p>
]]></description><link>https://forum.qt.io/post/607689</link><guid isPermaLink="true">https://forum.qt.io/post/607689</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 19 Jul 2020 17:59:50 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 22:53:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> thank you! I added in <strong>.pro</strong> file:</p>
<pre><code>DEFINES += QWT_DLL
</code></pre>
<p dir="auto">Could you please explain what this line means? I've found it in internet but I don't understand how it works</p>
]]></description><link>https://forum.qt.io/post/607626</link><guid isPermaLink="true">https://forum.qt.io/post/607626</guid><dc:creator><![CDATA[Please_Help_me_D]]></dc:creator><pubDate>Sat, 18 Jul 2020 22:53:08 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 16:54:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/please_help_me_d">@<bdi>Please_Help_me_D</bdi></a> Then you forgot to link against the qwt library.</p>
]]></description><link>https://forum.qt.io/post/607603</link><guid isPermaLink="true">https://forum.qt.io/post/607603</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 18 Jul 2020 16:54:05 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 16:04:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a> said in <a href="/post/607581">Check if widget is pushbutton</a>:</p>
<blockquote>
<p dir="auto">if (QString("QPushButton") == widget-&gt;metaObject()-&gt;className())<br />
...</p>
</blockquote>
<p dir="auto">this works, thank you!</p>
<p dir="auto">But when I try to execute:</p>
<pre><code>auto plot = qobject_cast&lt;QwtPlot*&gt;(widget);
</code></pre>
<p dir="auto">I get an error:<br />
<strong>mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject@@B)</strong></p>
<p dir="auto"><code>QwtPlot</code> is from <code>QWT</code> library. It is declared as:<br />
<code>class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict</code></p>
]]></description><link>https://forum.qt.io/post/607594</link><guid isPermaLink="true">https://forum.qt.io/post/607594</guid><dc:creator><![CDATA[Please_Help_me_D]]></dc:creator><pubDate>Sat, 18 Jul 2020 16:04:42 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 15:17:41 GMT]]></title><description><![CDATA[<p dir="auto">Ok I guess that eliminates all arguments for the string comparison...</p>
]]></description><link>https://forum.qt.io/post/607589</link><guid isPermaLink="true">https://forum.qt.io/post/607589</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Sat, 18 Jul 2020 15:17:41 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 15:12:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a> i just tested with qt5.14/minGw, it compiles and i don't see any warnings</p>
]]></description><link>https://forum.qt.io/post/607588</link><guid isPermaLink="true">https://forum.qt.io/post/607588</guid><dc:creator><![CDATA[ODБOï]]></dc:creator><pubDate>Sat, 18 Jul 2020 15:12:38 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:59:59 GMT]]></title><description><![CDATA[<p dir="auto">@LeLev sure but doesn't the compiler complain about '=' inside an if statement?</p>
]]></description><link>https://forum.qt.io/post/607586</link><guid isPermaLink="true">https://forum.qt.io/post/607586</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:59:59 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:53:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a> can't you do?</p>
<pre><code>if(auto pushButton = qobject_cast&lt;QPushButton*&gt;(pushButton ))
</code></pre>
]]></description><link>https://forum.qt.io/post/607585</link><guid isPermaLink="true">https://forum.qt.io/post/607585</guid><dc:creator><![CDATA[ODБOï]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:53:53 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:44:47 GMT]]></title><description><![CDATA[<p dir="auto">Yes it's better than string comparison, but it's 2 lines :-)</p>
<pre><code>auto pushButton = qobject_cast&lt;QPushButton*&gt;(widget);
if (nullptr != pushButton)
</code></pre>
]]></description><link>https://forum.qt.io/post/607584</link><guid isPermaLink="true">https://forum.qt.io/post/607584</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:44:47 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:38:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/please_help_me_d">@<bdi>Please_Help_me_D</bdi></a> hi,<br />
use <a href="https://doc.qt.io/qt-5/qobject.html#qobject_cast" target="_blank" rel="noopener noreferrer nofollow ugc">qobject_cast</a></p>
]]></description><link>https://forum.qt.io/post/607583</link><guid isPermaLink="true">https://forum.qt.io/post/607583</guid><dc:creator><![CDATA[ODБOï]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:38:55 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:35:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a> no, use proper c++ instead string comparison please.</p>
]]></description><link>https://forum.qt.io/post/607582</link><guid isPermaLink="true">https://forum.qt.io/post/607582</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:35:10 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:33:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi, you could try:</p>
<pre><code>if (QString("QPushButton") == widget-&gt;metaObject()-&gt;className())
...
</code></pre>
]]></description><link>https://forum.qt.io/post/607581</link><guid isPermaLink="true">https://forum.qt.io/post/607581</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:33:03 GMT</pubDate></item><item><title><![CDATA[Reply to Check if widget is pushbutton on Sat, 18 Jul 2020 14:26:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/please_help_me_d">@<bdi>Please_Help_me_D</bdi></a> said in <a href="/post/607573">Check if widget is pushbutton</a>:</p>
<blockquote>
<p dir="auto">doesn't seem to work...</p>
</blockquote>
<p dir="auto">Because you use s a <a href="https://en.cppreference.com/w/cpp/language/static_cast" target="_blank" rel="noopener noreferrer nofollow ugc">static_cast&lt;&gt;</a> which (what it says) statically casts a pointer into another one ... C++ basics.</p>
]]></description><link>https://forum.qt.io/post/607578</link><guid isPermaLink="true">https://forum.qt.io/post/607578</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 18 Jul 2020 14:26:25 GMT</pubDate></item></channel></rss>