Results 1 to 8 of 8

Thread: QMainWindow same value of widget->metaObject()->className() and widget->objectName()

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QMainWindow same value of widget->metaObject()->className() and widget->objectName()

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    Use setObjectName() to set an object name.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    Quote Originally Posted by tonnot View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    ->inherits("QMainWindow")) can be a solution.
    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    Not a bright one though. qobject_cast is much quicker. So are many other solutions that come to my mind.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    Quote Originally Posted by tonnot View Post
    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    tonnot (27th January 2012)

  9. #8
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMainWindow same value of widget->metaObject()->className() and widget->objectNam

    Thansk to both!

Similar Threads

  1. Replies: 4
    Last Post: 25th August 2014, 18:05
  2. Replies: 2
    Last Post: 29th June 2011, 15:45
  3. QMainWindow inside another widget
    By DanFessler in forum Newbie
    Replies: 4
    Last Post: 23rd August 2010, 21:58
  4. QMainWindow setCentralWidget from ui widget, Qt4
    By alan in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2008, 13:00
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.