Have a go hero – Implementing the custom rectangle as a class

In this example, we avoided creating a class for our custom rectangle to keep the code as simple as possible. Following the principles of object-oriented programming, subclassing QGraphicsRectItem and creating children items in the constructor of the new class is a good idea. Doing this doesn't require anything you don't already know. For example, when subclassing QGraphicsRectItem, you don't need to implement any virtual functions, because they are all properly implemented in the base classes.