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. [Solved] QGraphicsItem::boundingRect() and children
Forum Update on Monday, May 27th 2025

[Solved] QGraphicsItem::boundingRect() and children

Scheduled Pinned Locked Moved General and Desktop
qgraphicsitemboundingrect
5 Posts 3 Posters 3.2k 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    Is QGraphicsItem::boundingRect() supposed to return the bounding rect of just this item or does this need to include children items too?

    Let's assume that I have a QGraphicsRectItem with different children where the children are partially inside and partially outside of the rect items rectangle. Do I have to implement the boundingRect() method so it takes the children into account as well?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    1
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by alex_malyu
      #2

      You should return the bounding rect of all items.

      Have you seen QGraphicsItemGroup?
      This may provide an implementation you want.

      Joel BodenmannJ 2 Replies Last reply
      -1
      • A alex_malyu

        You should return the bounding rect of all items.

        Have you seen QGraphicsItemGroup?
        This may provide an implementation you want.

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by
        #3

        That is exactly what I need. Thank you very much!

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0
        • A alex_malyu

          You should return the bounding rect of all items.

          Have you seen QGraphicsItemGroup?
          This may provide an implementation you want.

          Joel BodenmannJ Offline
          Joel BodenmannJ Offline
          Joel Bodenmann
          wrote on last edited by
          #4

          I'm sorry to bring up this old topic. However, after three more years of experience working with Qt & the Qt graphics framework I'd like to say that the original answer is incorrect. QGraphicsItem::boundingRect() must not return a rectangle that also contains the children. It must only return that item's (*this) bounding rect without taking any children into account.
          If there's a need to retrieve the bounding rect of all children, there's QGraphicsItem::childrenBoundingRect(). That rectangle will be a union of the bounding rects of ALL children but it will not contain the calling item's bounding rect.

          The following code allows to retrieve the bounding rect of an item and all it's children:

          QRectF rectF = boundingRect();
          rectF = rectF.united(childrenBoundingRect());
          

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          A 1 Reply Last reply
          5
          • Joel BodenmannJ Joel Bodenmann

            I'm sorry to bring up this old topic. However, after three more years of experience working with Qt & the Qt graphics framework I'd like to say that the original answer is incorrect. QGraphicsItem::boundingRect() must not return a rectangle that also contains the children. It must only return that item's (*this) bounding rect without taking any children into account.
            If there's a need to retrieve the bounding rect of all children, there's QGraphicsItem::childrenBoundingRect(). That rectangle will be a union of the bounding rects of ALL children but it will not contain the calling item's bounding rect.

            The following code allows to retrieve the bounding rect of an item and all it's children:

            QRectF rectF = boundingRect();
            rectF = rectF.united(childrenBoundingRect());
            
            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @Joel-Bodenmann
            To elaborate on your answer: The boundingRect() must include every pixel that you intend to paint to in your own item's paint method. That includes e.g. pixels that are outside your item proper, but painted to due to a wide pen.
            (Calculating the correct boundingRect for items with wide cosmetic pens can be a real pain.)

            1 Reply Last reply
            1

            • Login

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