Results 1 to 4 of 4

Thread: qobject_cast for Qt3 ?

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default qobject_cast for Qt3 ?

    Hi Guys,

    Just thought like making a qobject_cast for Qt3.
    This template functon in Qt4 helps us avoid a lots of typo's So I just thought like making one. this is an intial draft, so please give your suggestions

    So here goes the qobject_cast
    Qt Code:
    1. template < typename T >
    2. inline T* qobject_cast( QObject *object ){
    3. QObject tmp;
    4. T instance( &tmp);
    5. if( object->inherits( instance.className()) ){
    6. return dynamic_cast< T* >( object );
    7. }
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Simple Test App
    Qt Code:
    1. #include <qpushbutton.h>
    2. #include <qapplication.h>
    3.  
    4. template < typename T >
    5. inline T* qobject_cast( QObject *object ){
    6. QObject tmp;
    7. T instance( &tmp);
    8. if( object->inherits( instance.className()) ){
    9. return dynamic_cast< T* >( object );
    10. }
    11. return 0;
    12. }
    13.  
    14. int main( int argc, char *argv[] ){
    15.  
    16. QApplication app( argc, argv );
    17. QObject *obj = new QPushButton(0, "Name");
    18.  
    19. QPushButton *b = qobject_cast<QPushButton>( obj );
    20. if( b ){
    21. qDebug( b->name() );
    22. }
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 
    My inital intend was to create a qobject_cast was
    QPushButton *b = qobject_cast< QPushButton * >( object );
    Note that the * is missing in the version I have written. Any Ideas ?
    Last edited by sunil.thaha; 15th December 2006 at 15:08. Reason: pasted wrong code
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Jan 2006
    Location
    Cambridge, MA
    Posts
    32
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qobject_cast for Qt3 ?

    qobject_cast was added to work around problems with dynamic cast over shard objects and the lack of dynamic_cast when RTTI is turned off. I think your code might still exhibit these issues. If you pass the moc inherits test I think you are safe with a static_cast<> which wil work.

    --Justin

  3. #3
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qobject_cast for Qt3 ?

    Quote Originally Posted by sunil.thaha View Post
    Hi Guys,

    Just thought like making a qobject_cast for Qt3.
    What is the difference between qtobject_cast in Qt4 and qt_cast in Qt3?

    qt_cast is defined in objectdefs.h

  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qobject_cast for Qt3 ?

    Hey that clue was good I looked in the qobjectdefs.h & then I stumbled upon staticMetaObject() atleast it is there in Qt 3.3.6

    So I re wrote the qobject_cast And Guess what ?? you can use qobject_cast< QClassName *>( object );

    And for qt_cast : I have no clue ( atleast for now )

    So here is the new Code qobject_cast
    Qt Code:
    1. #include <qmetaobject.h>
    2.  
    3. template < typename T >
    4. inline T qobject_cast( QObject *object ){
    5. T dummy;
    6. if( object->inherits( dummy->staticMetaObject()->className()) ){
    7. return static_cast< T >( object );
    8. }
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    And the Driver still remains the same for most of its part except that now we can use the cast similar to Qt4 's

    Qt Code:
    1. #include <qmetaobject.h> // staticMEtaObject used
    2.  
    3. #include <qpushbutton.h>
    4. #include <qcombobox.h>
    5. #include <qapplication.h>
    6.  
    7.  
    8. template < typename T >
    9. inline T qobject_cast( QObject *object ){
    10. T dummy;
    11. if( object->inherits( dummy->staticMetaObject()->className()) ){
    12. return static_cast< T >( object );
    13. }
    14. return 0;
    15. }
    16.  
    17. int main( int argc, char *argv[] ){
    18.  
    19. QApplication app( argc, argv );
    20. QObject *obj = new QPushButton(0, "Name");
    21.  
    22. QComboBox *b = qobject_cast<QComboBox*>( obj );
    23. if( b ){
    24. qDebug( b->name() );
    25. }else {
    26. qDebug( "Could not typcast ");
    27. }
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.