33

I'm writing a test app that simulates key presses of another application. For every key press I have to check if the right window/form is shown. So what I do is get the pointer of the window being shown and get it's window title. However, not all the windows/forms shown window titles. So I'm thinking it would be better to get the name of the class instead. How can I get the name of the class?

QWidget *pWin = QApplication::activeWindow();

when I try:

pWin->className(); 

to get the name of the class, I'm getting:

"error: class QWidget has no member named 'className' "

Can somebody show me the right way?

AAEM
  • 1,837
  • 2
  • 18
  • 26
Owen
  • 4,063
  • 17
  • 58
  • 78

2 Answers2

68

Try using the metaobject.

pWin->metaObject()->className();  
jonie83
  • 1,136
  • 2
  • 17
  • 28
J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
  • When I try this I get the class name QObject which is the inherited class name and not the name of the derived class. Any comments on how to get the derived class name? – Dean P Aug 17 '19 at 21:28
  • @DeanP You need to add the Q_OBJECT macro to your derived class in order for Qt to be aware of the derived class's name. – michaelmoo Nov 12 '20 at 22:45
0

You could also check the typeinfo header. Using the typeid operator on you object you get a type_info instance which describes the type of your object. Check out: http://www.cplusplus.com/reference/std/typeinfo/type_info/

Ioan Alexandru Cucu
  • 11,981
  • 6
  • 37
  • 39