Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. static_cast vs qobject_cast

static_cast vs qobject_cast

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 4.5k Views
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    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

    KroMignonK R JonBJ 4 Replies Last reply
    0
    • SPlattenS SPlatten

      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?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #2

      @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
      7
      • SPlattenS SPlatten

        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?

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        @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
        4
        • SPlattenS SPlatten

          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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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
          0
          • SPlattenS SPlatten

            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?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

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

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved