Monday, July 19, 2010

Creating object dynamically from class name in Qt

Some time it could be useful to be able to create new instance of class just by knowing its name. Such as in situation like when you are receiving XML from network and you want to create certain type of parser depending upon XML received from network.

In Qt its possible by using Qt's meta object system.

Here is my sample implementation that uses QMetaType to achieve the same. In sample code Parser is base class of all parser.
class Parser
{
public:

 virtual void parse() = 0;
 
 virtual ~Parser() {};

};
Now I am creating two dummy parser which are derived from Parser.
class Parser1 : public Parser
{
public:
    
    Parser1()
    {
        qDebug() <<"Parser1::Parser1()";
    }
    
    void parse()
    {
        qDebug() << "Parser1::parse()";
    }
    
    ~Parser1()
    {
       qDebug() <<"Parser1::~Parser1()";
    }
};
Q_DECLARE_METATYPE(Parser1)
  
class Parser2 : public Parser
{
public:
    
    Parser2()
    {
        qDebug() <<"Parser2::Parser2()";
    }
    
    void parse()
    {
        qDebug() << "Parser2::parse()";
    }
    
    ~Parser2()
    {
       qDebug() <<"Parser2::~Parser2()";
    }
};
Q_DECLARE_METATYPE(Parser2)
Now in parse function I am trying to create meta type from its name.
void parse( const char* parserName ) 
{
     int id = QMetaType::type( parserName );
     if (id != -1) {
        Parser *parser = static_cast< Parser* > 
                         ( QMetaType::construct( id ) );
        parser->parse();
        delete parser;
    }
}
Some sample code to test above function.
int main ( int argc, char* argv[] )
{
    qRegisterMetaType("Parser1");
    qRegisterMetaType("Parser2");
    
    qDebug() << "###### Trying create Parser1";
    parse("Parser1");
    
    qDebug() << "###### Trying create Parser2";
    parse("Parser2");
}
and finally output.
###### Trying create Parser1 
Parser1::Parser1() 
Parser1::parse() 
Parser1::~Parser1() 
###### Trying create Parser2 
Parser2::Parser2() 
Parser2::parse() 
Parser2::~Parser2() 
so that was all, but remember to use Q_DECLARE_METATYPE, qRegisterMetaType macros for registering your custom type with Qt meta object system.

No comments:

Post a Comment