#includeclass MyRect : public QGraphicsItem { public: MyRect(); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); }; MyRect::MyRect() { // ... } QRectF MyRect::boundingRect() const { // ... } void MyRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->fillRect(boundingRect(), Qt::red); }
#includeThis code uses the MyRect class from the previous example to create a red rectangle and adds it to a QGraphicsScene. The scene is then displayed using a QGraphicsView. This example also uses the Qt library.#include #include "myrect.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QGraphicsView view; QGraphicsScene scene; view.setScene(&scene); MyRect *rect = new MyRect(); scene.addItem(rect); view.show(); return a.exec(); }