You don't need any of these to put child items inside a parent item.
Each item, no matter what it is, has its own coordinate system. Its coordinate system is used to draw its own contents. Each item is sized and positioned according to the coordinate system of its parent item (or the scene if it is a top-level item).
So if you have a QGraphicsRectItem with the size (100, 100), then its coordinate system goes from (0,0) to (99, 99). The top left corner is (0,0) and the bottom right corner is (99, 99). If you want to add a QGraphicsEllipseItem as a child, you make the child whatever size you want it to be (in the coordinate system of the parent rectangle), and place it inside the rectangle relative to the top left corner of the rectangle. The (0,0) coordinate for the ellipse is the top left corner of its boundingRect() (the rectangle that encloses the ellipse, not the rectangle you are using for its parent).
If you want to place an ellipse exactly in the center of the parent QGraphicsRectObject, you have to offset the ellipse's position by the half of the width and half of the height of its boundingRect().
Edit: Actually, I am not sure if the origin (0,0) of a QGraphicsItem is at the top left or at the center. Someone with a better memory might like to correct me if needed.




Reply With Quote
Bookmarks