QT : get the class name of an object

北城余情 提交于 2019-11-29 01:47:05

问题


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?


回答1:


Try using the metaobject.

pWin->metaObject()->className();  



回答2:


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/



来源:https://stackoverflow.com/questions/4241976/qt-get-the-class-name-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!