Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved static_cast vs qobject_cast

    General and Desktop
    4
    5
    1536
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SPlatten
      SPlatten last edited by

      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?

      Kind Regards,
      Sy

      KroMignon R JonB 4 Replies Last reply Reply Quote 0
      • KroMignon
        KroMignon @SPlatten last edited by KroMignon

        @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.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply Reply Quote 7
        • R
          Robert Hairgrove @SPlatten last edited by

          @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.

          1 Reply Last reply Reply Quote 4
          • JonB
            JonB @SPlatten last edited by

            @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);
            
            1 Reply Last reply Reply Quote 0
            • KroMignon
              KroMignon @SPlatten last edited by KroMignon

              @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

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply Reply Quote 4
              • First post
                Last post