Time for action – creating a black, rectangular item
As a first approach, let's create an item that paints a black rectangle:
class BlackRectangle : public QGraphicsItem { public: explicit BlackRectangle(QGraphicsItem *parent = 0) : QGraphicsItem(parent) {} virtual ~BlackRectangle() {} QRectF boundingRect() const { return QRectF(0, 0, 75, 25); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) Q_UNUSED(widget) painter->fillRect(boundingRect(), Qt::black); } };
What just happened?
First, we subclass QGraphicItem
and call the new class BlackRectangle
. The class' constructor accepts a pointer to a QGraphicItem
item. This pointer is then passed to the constructor of the QGraphicItem
item. We ...
Get Game Programming Using Qt 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.