Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating objects of class through its name stored in a string?

Tags:

c++

qt

qt4

Can I create an object of a class at runtime, by extracting the class name stored in a string?

eg: I want to create and object of class QButton like

QString strClassName = "QButton";
QButton *pBtn = new strClassName();

I want to read an xml file of all the controls and instantiate them at runtime using this way.

like image 432
mots_g Avatar asked Jul 15 '10 11:07

mots_g


2 Answers

Maybe you are looking for the functionality provided by QUiLoader?

like image 140
Greg S Avatar answered Sep 18 '22 13:09

Greg S


In general, you might want to look at these. As another poster pointed out, this functionality exists in Qt for (at least) Qt classes.

Generic factory in C++

Is there a way to instantiate objects from a string holding their class name?

like image 27
mahju Avatar answered Sep 19 '22 13:09

mahju