Results 1 to 7 of 7

Thread: qobject_cast not working right?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qobject_cast not working right?

    qobject_cast is implemented this way:

    Qt Code:
    1. template <class T> inline T qobject_cast_helper(QObject *object, T)
    2. { return static_cast<T>(((T)0)->staticMetaObject.cast(object)); }
    3.  
    4. template <class T>
    5. inline T qobject_cast(QObject *object)
    6. { return qobject_cast_helper<T>(object, T(0)); }
    To copy to clipboard, switch view to plain text mode 

    So I guess it uses QMetaObject, whereas inherits() is simply:

    Qt Code:
    1. inline bool inherits(const char *classname) const
    2. { return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
    To copy to clipboard, switch view to plain text mode 
    What's funny, qt_metacast() is defined by... Q_OBJECT macro... and implemented by moc:

    Qt Code:
    1. void *MYCLASS::qt_metacast(const char *_clname)
    2. {
    3. if (!_clname) return 0;
    4. if (!strcmp(_clname, qt_meta_stringdata_MYCLASS))
    5. return static_cast<void*>(const_cast<MYCLASS*>(this));
    6. if (!strcmp(_clname, "Ui::MYCLASS"))
    7. return static_cast<Ui::MYCLASS*>(const_cast<MYCLASS*>(this));
    8. return BASECLASS::qt_metacast(_clname);
    9. }
    To copy to clipboard, switch view to plain text mode 

    So... the bottom line is... if inherits() works for you, it's only because the base class (QObject?) has it implemented, but my guess is that it won't be possible to check if some subclass of MYCLASS inherits MYCLASS without the Q_OBJECT macro inside MYCLASS.

  2. The following user says thank you to wysota for this useful post:

    Mike (30th May 2006)

  3. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: qobject_cast not working right?

    Great stuff... many thanks for explaining that.

Similar Threads

  1. Mac OS X UI not working
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 01:02
  2. Signals/Slots stopped working
    By Jimmy2775 in forum Qt Programming
    Replies: 8
    Last Post: 31st March 2006, 21:11
  3. Working with Date type in database applications
    By kolach in forum Qt Programming
    Replies: 13
    Last Post: 25th January 2006, 14:20
  4. QT Service and working directory
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 11:46
  5. QSettings - beginReadArray not working
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 9th January 2006, 21:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.