1

I've a custom QGraphicsItem which draws nothing but is parent to other QGraphicsItems (like QGraphicsRectItem and so on). My top-level item has (0,0) somewhere "inside" the children Items. This is very inconvenient. I would like to shift the origin to the upper left corner of childrenBoundingRect().

In the picture I've (0,0) of my top-level item somewhere inside of my children items (solid arrow). I would like to shift the origin to the dashed lines. How can I do that?

As result I expect that positioning of the top-level item will be more convenient.

enter image description here

dani
  • 3,677
  • 4
  • 26
  • 60

1 Answers1

1

Examples for overriding QGraphicsItem often show the boundingRect() function originating from (0,0). Changing this will change the origin. So, for example, to change it to the centre, where width and height are variables stored internally in the class, you can do this: -

QRectF boundingRect() const
{
    return (-width / 2, -height / 2, width, height);
}
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Thank you for your answer. According to this question, an item should only return it's own boundingRect because of redrawing: http://stackoverflow.com/questions/7448321/should-qgraphicsitemboundingrect-include-child-bounding-rects I will that that and see which way I go. – dani May 27 '16 at 15:45
  • I'm not suggesting returning the bounding rect of another item, just the child items in this case. – TheDarkKnight May 31 '16 at 08:39