7

I'm looking for something like this:

MyClass::metaObject()->className()

which doesn't work because at the point where this code is executed, there exists no instantiation of MyClass.

If this is somehow possible, is there a way of getting all names of the classes that have been derived from MyClass?

jrok
  • 54,456
  • 9
  • 109
  • 141
Marc
  • 767
  • 1
  • 10
  • 23

2 Answers2

19

Use the static meta object:

 MyClass::staticMetaObject.className()

Works!

Marc
  • 767
  • 1
  • 10
  • 23
BeniBela
  • 16,412
  • 4
  • 45
  • 52
  • 3
    @TomášZato Just use `myClassInstance->staticMetaObject().className()`. – rbaleksandar Jan 31 '17 at 15:14
  • 9
    @TomášZato Actually, you probably want to use `metaObject` rather than `staticMetaObject`, to get info about the actual type of the object and not the type of the pointer. – Luc Touraille Jun 14 '17 at 08:36
5

You should be able to use:

obj->metaObject()->className();
Hans
  • 81
  • 1
  • 3
  • This gave me the desired result by returning the name of the actual class and not the base class (as oposed to the accepted answer) – George Oct 20 '21 at 22:40