If you want to keep a limited scope, you can override ItemChanged ()
Declare:
#ifndef GRAPHIC_H #define GRAPHIC_H #include <QGraphicsRectItem> class Graphic : public QGraphicsRectItem { public: Graphic(const QRectF & rect, QGraphicsItem * parent = 0); protected: virtual QVariant itemChange ( GraphicsItemChange change, const QVariant & value ); }; #endif // GRAPHIC_H
implementation : The ItemSendsGeometryChanges flag is needed to record a change in position of a QGraphicsItem
#include "graphic.h"
Then we define the rectangle of the scene, in this case there will be 300x300
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QGraphicsView * view = new QGraphicsView(this); QGraphicsScene * scene = new QGraphicsScene(view); scene->setSceneRect(0,0,300,300); view->setScene(scene); setCentralWidget(view); resize(400,400); Graphic * graphic = new Graphic(QRectF(0,0,100,100)); scene->addItem(graphic); graphic->setPos(150,150); }
This means that the graph is inside the area, good luck
source share