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. [Solved] Warning C4946: reinterpret_cast used between related classes when using qobject_cast
Forum Updated to NodeBB v4.3 + New Features

[Solved] Warning C4946: reinterpret_cast used between related classes when using qobject_cast

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 6.1k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    d.oberkofler
    wrote on last edited by
    #1

    I'm consistently getting "warning C4946: reinterpret_cast used between related classes: 'QObject' and 'QLabel'" when compiling code like the following in VC7:

    @void myFunction(QWidget* theWidget)
    {
    QLabel* aLabel = qobject_cast<QLabel*>(theWidget);
    if (aLabel)
    {
    ...
    }
    }@

    I'm having a hard time to understand why qobject_cast would issues this warning and was hoping for some help to better understand what I'm doing/using wrong?

    I (always) compile on the Windows platform with the -Wall and -WX options.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      It is because of nature of qobject_cast. It doesn't use dynamic_cast, but makes checks with metaobject system. If classes are compatible, then this cast simply converts object to destination class and returns it.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        d.oberkofler
        wrote on last edited by
        #3

        I'm not sure if I understand the consequences:

        • Does this mean that I should use a dynamic_cast instead of qobject_cast? to be warning free?
        • Is this a small implementation error on the Qt side because they wrongly use reinterpret_cast?
        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          d.oberkofler: Neither.

          There is no need to avoid qobject_casts and there is no implementation error in Qt (at least not there;-). Since Qt can work without RTTI (runtime type information) it can not dynamic_cast and it is not necessary to do so as qobject_cast makes sure a reinterpret cast will work by checking the Qt meta object data.

          Qt headers do trigger warnings when compiled with a high enough warning settings. So far all I looked into where false positives (like this one you report here). If you want to use high warning levels and still build using Qt then you might want to consider telling your compiler that the Qt is a system library (it will not generate warnings for those). With gcc this is e.g. done by using the -isystem /path/to/qt.

          1 Reply Last reply
          0

          • Login

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