Results 1 to 7 of 7

Thread: qobject_cast not working right?

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

    Question qobject_cast not working right?

    Hi,

    I'm using QT 4.1 and can't explain the following problem. I have a method that does return either Class1 or Class2. Both inherit QObject. If I read the manual correct, than the following should result in null, but it doesn't:
    Qt Code:
    1. ...
    2. public class Class1 : public QObject
    3. {
    4. ...
    5. };
    6.  
    7. public class Class2 : public QObject
    8. {
    9. ...
    10. };
    11.  
    12. public QObject* GetSomeData()
    13. {
    14. return new Class2();
    15. }
    16.  
    17.  
    18. ....
    19. // Somewhere I invoke the method above and would like
    20. // to check if the method returned Class1 or Class2...
    21.  
    22. QObject* response = GetSomeData();
    23. Class1* classOne = qobject_cast<Class1*>(response);
    24. if (classOne)
    25. {
    26. // Method returned instance of class 1
    27. ...
    28. }
    To copy to clipboard, switch view to plain text mode 

    So the problem I have in my code, is that qobject_cast never results in a null pointer... So the following if condition does allways execute... however the documentation does say something else. Anybody seen this too?

    Regards,
    Michael

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qobject_cast not working right?

    Do you have Q_OBJECT macro in both Class1 and Class2 definition?

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

    Default Re: qobject_cast not working right?

    Matter of fact, I haven't. Is it required for the cast to work? Those classes do not have signals or slots and are just data containers. But from your comment I assume it is required to have the marco defined?

  4. #4
    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?

    It's required so that meta-type data (for example the name and descendance tree of the class, so that it can be cast using QObject::inherits) can be associated with the class.

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

    Default Re: qobject_cast not working right?

    Ok, but what's funny then is that I changed my code because of my problem to invoke
    Qt Code:
    1. ...
    2. response->inherits("Class1")
    3. ...
    To copy to clipboard, switch view to plain text mode 

    as you mentioned, and inherits does work just fine. It's just the cast that's failing.

    Thanks anyway.

  6. #6
    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.

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

    Mike (30th May 2006)

  8. #7
    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.