Qt World Summit: Submit your Presentation

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

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

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

  • 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?

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

Log in to reply