Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Differences between qgraphicsitem_cast and dynamic_cast

Differences between qgraphicsitem_cast and dynamic_cast

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 735 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • michalmondayM Offline
    michalmondayM Offline
    michalmonday
    wrote on last edited by
    #1

    Is there any reason for using qgraphicsitem_cast instead of dynamic_cast for recognizing sublass of QGraphicsItem?

    qgraphicsitem_cast requires implementing "type()" method, and dynamic_cast doesn't require that (so dynamic_cast is simpler to use from my point of view), but is there any drawbacks to using dynamic_cast?

    That's how I'm recognizing subclasses currently:

    for (QGraphicsItem *item : childItems()) // (childItems of some parent GraphicsItem)
            if (SubclassGraphicsItem *sub_item = dynamic_cast<SubclassGraphicsItem *>(item))
                    //use sub_item
    

    Is there some benefits to using qgraphicsitem_cast that will make it worth to implement "type()" method for each of the QGraphicsItem subclasses?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      qgraphicsitem_cast basically does if (item->type() == T::Type) return static_cast<T>(item) so potentially a lot faster than dynamic_cast, which needs to do runtime RTTI traversal.

      If you do that check often it's definitely worth it.

      1 Reply Last reply
      4

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved