Time for action – Using child items

Let's try to make an item containing multiple children. We want to create a rectangle with a filled circle in each corner and be able to move and rotate it as a whole, like this:

First, you need to create a function that creates a single complex rectangle, by using the following code:

QGraphicsRectItem *createComplexItem(    qreal width, qreal height, qreal radius) { QRectF rect(-width / 2, -height / 2, width, height); QGraphicsRectItem *parent = new QGraphicsRectItem(rect); QRectF circleBoundary(-radius, -radius, 2 * radius, 2 * radius); for(int i = 0; i < 4; i++) { QGraphicsEllipseItem *child = new QGraphicsEllipseItem(circleBoundary, ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.