Results 1 to 3 of 3

Thread: What's the difference between qt cast from normal C++ cast

  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default What's the difference between qt cast from normal C++ cast

    I wonder what's the difference between qt cast like qgraphicsitem_cast, qobject_cast, qvariant_cast from normal C++ cast like "(QGraphicsRectItem *)item"?
    Thanks in advance.

  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: What's the difference between qt cast from normal C++ cast

    Quote Originally Posted by Seishin View Post
    I wonder what's the difference between qt cast like qgraphicsitem_cast, qobject_cast, qvariant_cast from normal C++ cast like "(QGraphicsRectItem *)item"?
    Thanks in advance.
    "(QGraphicsRectItem*)item" is not a "normal C++ cast", it's an ugly C cast. In C++ it should be static_cast<QGraphicsRectItem*>(item) or reinterpret_cast<QGraphicsRectItem*>(item).

    Anyway... different casts use different techniques for checking if the cast is valid or not. qobject_cast uses information from QMetaObject, qgraphicsitem_cast uses QGraphicsItem::type(), qvariant_cast uses QVariant::type(). C++ has also another important cast -- dynamic_cast that uses RTTI information provided by the compiler.
    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. The following user says thank you to wysota for this useful post:

    Seishin (23rd November 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's the difference between qt cast from normal C++ cast

    Thanks for the information. Just restudied basic c++ casting. This is really helpful.

Similar Threads

  1. QMetaObject cast Error
    By nirab_25 in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2010, 08:58
  2. Do a special cast with qobject_cast
    By crazy_inf in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2010, 16:07
  3. Why doesn't this cast?
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 19:28
  4. cast
    By mickey in forum General Programming
    Replies: 1
    Last Post: 12th July 2006, 11:10
  5. Cast problem
    By yellowmat in forum Newbie
    Replies: 3
    Last Post: 7th February 2006, 15:57

Tags for this Thread

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.