static_cast vs qobject_cast
-
Today I took advice and replaced usage of static_cast with qobject_cast and found at least one problem. In my code I create nodes, a node contains all the attributes for a GUI object.
In one part of my code when a node is a Window type, I was doing the following:
if ( pobjWidget->isWindow() == true ) { QMainWindow* pWindow = static_cast<QMainWindow*>(pobjWidget); if ( pWindow != nullptr ) { if (strTitle.isEmpty() != true ) { pWindow->setWindowTitle(strTitle); } if ( intHeight > 0 ) { pWindow->setFixedHeight(intHeight); } if ( intWidth > 0 ) { pWindow->setFixedWidth(intWidth); } } }
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
There is no warnings or errors using either but it works find when using static_cast and not with qobject_cast.
Why?
-
Today I took advice and replaced usage of static_cast with qobject_cast and found at least one problem. In my code I create nodes, a node contains all the attributes for a GUI object.
In one part of my code when a node is a Window type, I was doing the following:
if ( pobjWidget->isWindow() == true ) { QMainWindow* pWindow = static_cast<QMainWindow*>(pobjWidget); if ( pWindow != nullptr ) { if (strTitle.isEmpty() != true ) { pWindow->setWindowTitle(strTitle); } if ( intHeight > 0 ) { pWindow->setFixedHeight(intHeight); } if ( intWidth > 0 ) { pWindow->setFixedWidth(intWidth); } } }
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
There is no warnings or errors using either but it works find when using static_cast and not with qobject_cast.
Why?
@SPlatten said in static_cast vs qobject_cast:
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
There is no warnings or errors using either but it works find when using static_cast and not with qobject_cast.
Why?First, qobject_cast is NOT the same as static_cast, it is most like dynamic_cast.
The difference with dynamic_cast, is that it does NOT require RTTI (Run-Time Type Information), so it works with any C++ compiler. The only restriction is that the class must inherit in any way (direct or indirect) from QObject andQ_OBJECT
macro must be declared. -
Today I took advice and replaced usage of static_cast with qobject_cast and found at least one problem. In my code I create nodes, a node contains all the attributes for a GUI object.
In one part of my code when a node is a Window type, I was doing the following:
if ( pobjWidget->isWindow() == true ) { QMainWindow* pWindow = static_cast<QMainWindow*>(pobjWidget); if ( pWindow != nullptr ) { if (strTitle.isEmpty() != true ) { pWindow->setWindowTitle(strTitle); } if ( intHeight > 0 ) { pWindow->setFixedHeight(intHeight); } if ( intWidth > 0 ) { pWindow->setFixedWidth(intWidth); } } }
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
There is no warnings or errors using either but it works find when using static_cast and not with qobject_cast.
Why?
@SPlatten If the declared or derived type of
pobjWidget
is actuallyQWidget
, you don't need to cast it at all because you are only calling member functions ofQWidget
here. What is the actual type of your object pointer?If your widget is not derived somehow from
QMainWindow
(directly or indirectly) but from some other class, such asQWindow
orQDialog
,qobject_cast
will return NULL when you try to cast the pointer toQMainWindow*
. But theQWidget::isWindow()
function returnstrue
for other types such as these, too. -
Today I took advice and replaced usage of static_cast with qobject_cast and found at least one problem. In my code I create nodes, a node contains all the attributes for a GUI object.
In one part of my code when a node is a Window type, I was doing the following:
if ( pobjWidget->isWindow() == true ) { QMainWindow* pWindow = static_cast<QMainWindow*>(pobjWidget); if ( pWindow != nullptr ) { if (strTitle.isEmpty() != true ) { pWindow->setWindowTitle(strTitle); } if ( intHeight > 0 ) { pWindow->setFixedHeight(intHeight); } if ( intWidth > 0 ) { pWindow->setFixedWidth(intWidth); } } }
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
There is no warnings or errors using either but it works find when using static_cast and not with qobject_cast.
Why?
-
Today I took advice and replaced usage of static_cast with qobject_cast and found at least one problem. In my code I create nodes, a node contains all the attributes for a GUI object.
In one part of my code when a node is a Window type, I was doing the following:
if ( pobjWidget->isWindow() == true ) { QMainWindow* pWindow = static_cast<QMainWindow*>(pobjWidget); if ( pWindow != nullptr ) { if (strTitle.isEmpty() != true ) { pWindow->setWindowTitle(strTitle); } if ( intHeight > 0 ) { pWindow->setFixedHeight(intHeight); } if ( intWidth > 0 ) { pWindow->setFixedWidth(intWidth); } } }
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
There is no warnings or errors using either but it works find when using static_cast and not with qobject_cast.
Why?
@SPlatten said in static_cast vs qobject_cast:
When using static_cast this works, however when I replaced static_cast with qobject_cast, pWindow is nullptr.
I don't have the same analyse as you because static_cast<>() does not check if destination type is the right one,
pWindow = static_cast<QMainWindow*>(pobjWidget)
is closely the same as doingpWindow = (QMainWindow*)(pobjWidget)
.So if
pobjWidget
is not null,pWindow
also won't be null, but this does not mean that cast is valid!If
qobject_cast<QMainWindow*>()
return null, this could be because:- it is the wrong type!
- the class used to instance
pobjWidget
does not inherit fromQObject
or does not have aQ_OBJECT
macro declared in his header