sunil.thaha
15th December 2006, 13:50
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
template < typename T >
inline T* qobject_cast( QObject *object ){
QObject tmp;
T instance( &tmp);
if( object->inherits( instance.className()) ){
return dynamic_cast< T* >( object );
}
return 0;
}
Simple Test App
#include <qpushbutton.h>
#include <qapplication.h>
template < typename T >
inline T* qobject_cast( QObject *object ){
QObject tmp;
T instance( &tmp);
if( object->inherits( instance.className()) ){
return dynamic_cast< T* >( object );
}
return 0;
}
int main( int argc, char *argv[] ){
QApplication app( argc, argv );
QObject *obj = new QPushButton(0, "Name");
QPushButton *b = qobject_cast<QPushButton>( obj );
if( b ){
qDebug( b->name() );
}
return 0;
}
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 ?
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
template < typename T >
inline T* qobject_cast( QObject *object ){
QObject tmp;
T instance( &tmp);
if( object->inherits( instance.className()) ){
return dynamic_cast< T* >( object );
}
return 0;
}
Simple Test App
#include <qpushbutton.h>
#include <qapplication.h>
template < typename T >
inline T* qobject_cast( QObject *object ){
QObject tmp;
T instance( &tmp);
if( object->inherits( instance.className()) ){
return dynamic_cast< T* >( object );
}
return 0;
}
int main( int argc, char *argv[] ){
QApplication app( argc, argv );
QObject *obj = new QPushButton(0, "Name");
QPushButton *b = qobject_cast<QPushButton>( obj );
if( b ){
qDebug( b->name() );
}
return 0;
}
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 ?