6

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.

jesterjunk
  • 2,184
  • 19
  • 17
mots_g
  • 637
  • 7
  • 27

2 Answers2

8

Maybe you are looking for the functionality provided by QUiLoader?

jesterjunk
  • 2,184
  • 19
  • 17
Greg S
  • 11,793
  • 2
  • 38
  • 48
2

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?

Community
  • 1
  • 1
mahju
  • 1,161
  • 1
  • 14
  • 21