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. <mysterious but solved> Trouble using qobject_cast with const QObject*
Forum Updated to NodeBB v4.3 + New Features

<mysterious but solved> Trouble using qobject_cast with const QObject*

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    I am using Q_DECLARE_INTERFACE / Q_INTERFACES in order to be able to use qobject_cast for objects which are not QObjects. The following works fine:

    @class ITsc_SnapEnabledContainer
    {

    public:
    // Some pure virtual methods that don't matter here

    // Helper to cast QObject to this interface
    static ITsc_SnapEnabledContainer* castToSnapEnabledContainer(QObject* pObject);
    

    };

    Q_DECLARE_INTERFACE(ITsc_SnapEnabledContainer, "ITsc_SnapEnabledContainer")@

    ...and in the cpp:

    @ITsc_SnapEnabledContainer* ITsc_SnapEnabledContainer::castToSnapEnabledContainer(QObject* pObject)
    {
    return qobject_cast<ITsc_SnapEnabledContainer*>(pObject);
    }@

    I have been using these types of cast operations for a long time, and never had any trouble.
    Until I tried to add this code:

    Header:
    @
    // Const overload
    static const ITsc_SnapEnabledContainer* castToSnapEnabledContainer(const QObject* pObject);
    @

    cpp:
    @
    const ITsc_SnapEnabledContainer* ITsc_SnapEnabledContainer::castToSnapEnabledContainer(const QObject* pObject)
    {
    return qobject_cast<const ITsc_SnapEnabledContainer*>(pObject);
    }
    @

    As soon as I add this, I get errors:
    @Error 1 error C2039: 'qt_check_for_QOBJECT_macro' : is not a member of 'ITsc_SnapEnabledContainer' c:\qtvs2010\4.8.1\src\corelib\kernel\qobject.h 368
    Error 2 error C2039: 'staticMetaObject' : is not a member of 'ITsc_SnapEnabledContainer' c:\qtvs2010\4.8.1\src\corelib\kernel\qobject.h 370
    Error 3 error C2228: left of '.cast' must have class/struct/union c:\qtvs2010\4.8.1\src\corelib\kernel\qobject.h 370
    @

    Any ideas?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Add Q_OBJECT macro in your class (don't forget to run qmake), qobject_cast needs that macro, see the documentation "here":http://qt-project.org/doc/qt-5/qobject.html#qobject_cast

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        In this case, it is not a QObject-derived class. The qobject_cast should work based on the Q_DECLARE_INTERFACE / Q_INTERFACES macros.

        See "docs on qobject_cast":http://qt-project.org/doc/qt-4.8/qobject.html#qobject_cast

        "qobject_cast() can also be used in conjunction with interfaces; see the Plug & Paint example for details."

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          The following works, but I wonder why:

          @const ITsc_SnapEnabledContainer* ITsc_SnapEnabledContainer::castToSnapEnabledContainer(const QObject* pObject)
          {
          return qobject_cast<ITsc_SnapEnabledContainer*>(pObject);
          }@

          It looks like I can take a const-QObject, make a non-const qobject_cast to return a const interface. Strange.

          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