Results 1 to 3 of 3

Thread: What is more efficient (qobject_cast or metaObject()->className())?

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question What is more efficient (qobject_cast or metaObject()->className())?

    Hi all,

    I have two versions of a code to detect if myWidget is a QCheckBox:

    Qt Code:
    1. QCheckBox* checkBox = qobject_cast<QCheckBox*>(myWidget);
    2. if (checkBox)
    3. {
    4. // do something
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. if ("QCheckBox" == QString(myWidget->metaObject()->className()))
    2. {
    3. // do something
    4. }
    To copy to clipboard, switch view to plain text mode 


    What version is more efficient?

    Best regards.

  2. #2
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What is more efficient (qobject_cast or metaObject()->className())?

    The best way is to benchmark it :P

    The common way is the first, for a number of reasons:
    - String compare is a slow task.
    - You can easily fail when writing "QCheckBox" as a string. If you do, compiler won't tell you. If you mistype QCheckBox as a type inside qobject_cast, the compiler will complain.
    - Using qobject_cast, you can access to specific members of QCheckBox if you need later on.

  3. #3
    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: What is more efficient (qobject_cast or metaObject()->className())?

    The difference in performance is so small, it is not worth considering. qobject_cast seems to be the more "proper" approach though in general.

    Having said that I'll also add that if "do something" doesn't require anything from QCheckBox API then the whole if block is wrong and should be replaced with some virtual function call.
    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.


Similar Threads

  1. Replies: 0
    Last Post: 20th March 2013, 10:02
  2. Replies: 7
    Last Post: 27th January 2012, 07:30
  3. set own Windows ClassName
    By JuanMO in forum Qt Programming
    Replies: 0
    Last Post: 19th November 2010, 12:10
  4. Y is className a member function rather than a static one ?
    By sunil.thaha in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2007, 00:43
  5. .ui file name and classname
    By Rekha in forum Newbie
    Replies: 3
    Last Post: 12th August 2006, 01:53

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.