[Qt-interest] Dynamically instantiate a class using the class name

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Mon Jul 5 16:05:37 CEST 2010


>>
>> In java, Class.forName(String className) provides this functiOn Mon, Jul 5, 2010 at 6:59 PM, Andre Somers <andre at familiesomers.nl> wrote:
> On 5-7-2010 15:19, Mandeep Sandhu wrote:
>> Hi All,
>>
>> Is there a way to programmatically  instantiate a class given it's name?
>>
>> Eg: I'm making a library which calls upon other helper classes to do
>> the actual job. So a user calls a method say - setHelper(QString
>> helpername) in this lib, and the lib internally instantiates an object
>> of a class who's name is specified in "helpername". I'll have the
>> "helper" classes follow a set interface so that my lib routines have a
>> uniform way of interacting with all such helpers.
>>
>> Does the QT meta system provide such a feature?
onality.
>> Is there something similar in QT too?
>>
> To me, it sounds like you are looking for the Factory pattern.

Yes. Is there a way to instantiate QObjects (and their subclasses),
given their name?

If not, how would I handle the above example?

I do not want to create all the helper objects before hand. I simply
want to keep a map of the helper name to a class name. Then when
someone requests functionality from helper class, I use the key to get
the helper class's name and then instantiate the helper to do the
required functionality.

I got one pure C++ soln here:
http://stackoverflow.com/questions/582331/c-is-there-a-way-to-instantiate-objects-from-a-string-holding-their-class-name

Is there any feature of QT that can help here?

Thanks,
-mandeep


>
> André
>
> --
> Nokia Certified Qt Developer.
> Interested in Qt related job offers or freelance Qt consulting work.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>




More information about the Qt-interest-old mailing list