PDA

View Full Version : QMainWindow same value of widget->metaObject()->className() and widget->objectName()



tonnot
17th January 2012, 10:09
I have the same value for :
widget->metaObject()->className()
widget->objectName()
The widget is a QMainWindow, I'm trying to distinguish betwen QMainWindow and other Qdialogs, but in case of the widget was a QMainWindow I have "Ui_main" for both.
Any help ?
Thanks.

wysota
17th January 2012, 11:09
Use setObjectName() to set an object name.

tonnot
17th January 2012, 15:21
Then is it a bug ? Why the classname is not "QMainWindow". And .... is there any other property I could use to detect QMainWindows ?
Thanks Wy.

wysota
17th January 2012, 16:14
Then is it a bug ?
What bug?


Why the classname is not "QMainWindow"
Is there a Q_OBJECT macro in your class?


And .... is there any other property I could use to detect QMainWindows ?
Sure, there are numerous ways possible... like using qobject_cast or using C++ properly.

tonnot
17th January 2012, 16:49
->inherits("QMainWindow")) can be a solution.
Thanks.

wysota
17th January 2012, 21:24
Not a bright one though. qobject_cast is much quicker. So are many other solutions that come to my mind.

ChrisW67
17th January 2012, 22:06
Then is it a bug ?
Of course not.

Why the classname is not "QMainWindow".
The property is called objectName and you are asking why it is not the class name. There can be several objects of class QMainWindow, or TonnotsMainWIndow or whatever, and each can have its own name, set with setObjectName(), in order that you can use the name to identify them. This would not be very useful if they were all fixed at "QMainWindow".

If you need to use object name to determine the type of an object you are probably going about things the wrong way. If you are trying to find the main window of the app in an object tree then you are better off keeping a pointer initialised at creation. If you have a routine that accepts any QWidget* and need to do something different for QMainWindow derivatives passed in then use qobject_cast<QMainWindow*>(p) and check the returned pointer.

tonnot
27th January 2012, 07:30
Thansk to both!