Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Differences between qgraphicsitem_cast and dynamic_cast

    General and Desktop
    2
    2
    325
    Loading More Posts
    • 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.
    • michalmonday
      michalmonday last edited by

      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 Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 3
        • First post
          Last post