Check if widget is pushbutton
-
Hi,
I have a widget
QWidget*and I need to check is that widget is aQPushButton*(orQFrame*or any inherited fromQWidget*). How to do that? The code:if (static_cast<QPushButton*>(widget)){ .... }doesn't seem to work...
-
@Please_Help_me_D said in Check if widget is pushbutton:
doesn't seem to work...
Because you use s a static_cast<> which (what it says) statically casts a pointer into another one ... C++ basics.
-
@hskoglund no, use proper c++ instead string comparison please.
-
Hi,
I have a widget
QWidget*and I need to check is that widget is aQPushButton*(orQFrame*or any inherited fromQWidget*). How to do that? The code:if (static_cast<QPushButton*>(widget)){ .... }doesn't seem to work...
@Please_Help_me_D hi,
use qobject_cast -
Yes it's better than string comparison, but it's 2 lines :-)
auto pushButton = qobject_cast<QPushButton*>(widget); if (nullptr != pushButton)@hskoglund can't you do?
if(auto pushButton = qobject_cast<QPushButton*>(pushButton )) -
@hskoglund can't you do?
if(auto pushButton = qobject_cast<QPushButton*>(pushButton )) -
@hskoglund i just tested with qt5.14/minGw, it compiles and i don't see any warnings
-
@hskoglund said in Check if widget is pushbutton:
if (QString("QPushButton") == widget->metaObject()->className())
...this works, thank you!
But when I try to execute:
auto plot = qobject_cast<QwtPlot*>(widget);I get an error:
mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject@@B)QwtPlotis fromQWTlibrary. It is declared as:
class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict -
@hskoglund said in Check if widget is pushbutton:
if (QString("QPushButton") == widget->metaObject()->className())
...this works, thank you!
But when I try to execute:
auto plot = qobject_cast<QwtPlot*>(widget);I get an error:
mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject@@B)QwtPlotis fromQWTlibrary. It is declared as:
class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict@Please_Help_me_D Then you forgot to link against the qwt library.
-
@Please_Help_me_D Then you forgot to link against the qwt library.
@Christian-Ehrlicher thank you! I added in .pro file:
DEFINES += QWT_DLLCould you please explain what this line means? I've found it in internet but I don't understand how it works
-
Even it works you should follow the official documentation: https://qwt.sourceforge.io/qwtinstall.html#COMPILEANDLINKAPP
-
@hskoglund can't you do?
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))@LeLev said in Check if widget is pushbutton:
@hskoglund can't you do?
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))It looks like several C++ features got mixed up in this post. The original question wasn't too far off:
if (static_cast<QPushButton*>(widget))The only problem here is that the wrong type of cast was used.
static_cast<>()means: "I know what I am doing.", i.e. assume that the cast is always allowed. The correct C++ feature isdynamic_cast<>(). So, a C++-only solution would look like this:if(dynamic_cast<QPushButton*>(widget))In the context of Qt it was correctly pointed out that
qobject_cast<>()is preferrable overdynamic_cast<>()(as it might be faster and does not need RTTI). A normal replacement of the cast is the easiest solution here:if(qobject_cast<QPushButton*>(widget))I am not entirely sure what C++ feature would enable the variant suggested by @LeLev :
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))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:
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ); pushButton) // this works since C++17Has anyone any idea why the version suggested by @LeLev would work?
-
@LeLev said in Check if widget is pushbutton:
@hskoglund can't you do?
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))It looks like several C++ features got mixed up in this post. The original question wasn't too far off:
if (static_cast<QPushButton*>(widget))The only problem here is that the wrong type of cast was used.
static_cast<>()means: "I know what I am doing.", i.e. assume that the cast is always allowed. The correct C++ feature isdynamic_cast<>(). So, a C++-only solution would look like this:if(dynamic_cast<QPushButton*>(widget))In the context of Qt it was correctly pointed out that
qobject_cast<>()is preferrable overdynamic_cast<>()(as it might be faster and does not need RTTI). A normal replacement of the cast is the easiest solution here:if(qobject_cast<QPushButton*>(widget))I am not entirely sure what C++ feature would enable the variant suggested by @LeLev :
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))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:
if(auto pushButton = qobject_cast<QPushButton*>(pushButton ); pushButton) // this works since C++17Has anyone any idea why the version suggested by @LeLev would work?
@SimonSchroeder said in Check if widget is pushbutton:
Has anyone any idea why the version suggested by @LeLev would work?
I've been using this feature for many years, it's definitely NOT a C++17 feature.
As I look into https://en.cppreference.com/w/cpp/language/if , it is calleddeclaration of a single non-array variable with a brace-or-equals initializer