Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?



  • @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 and Q_OBJECT macro must be declared.



  • @SPlatten If the declared or derived type of pobjWidget is actually QWidget, you don't need to cast it at all because you are only calling member functions of QWidget 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 as QWindow or QDialog, qobject_cast will return NULL when you try to cast the pointer to QMainWindow*. But the QWidget::isWindow() function returns true for other types such as these, too.



  • @SPlatten
    As @KroMignon said:

    First, qobject_cast is NOT the same as static_cast, it is most like dynamic_cast.

    The closest I know of if you want to replace static_cast<> behaviour is always to use:

    p = qobject_cast<>();
    Q_ASSERT(p);
    


  • @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 doing pWindow = (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 from QObject or does not have a Q_OBJECT macro declared in his header

Log in to reply