2

I have a QGraphicsView and a QGraphicsScene called scene with scene->setSceneRect(0,0,600, 600).

I have created a simple custom QGraphicsItem called background with boundingRectangle(0,0,600,600).

Very clearly the center of the background item is (300,300) with minX=0, minY=0 and maxX=600, maxY=600...but I want the center of this background item to be (0,0) with minX=-300, minY=-300 and origin at(0,0) and maxX=300, maxY=300. In other words I want the background items local co-ordinates system to reflect the natural co-ordinate system that we draw on paper.

x,y graph
(source: shmoop.com)

How do I do this.

Community
  • 1
  • 1
Anjanu
  • 623
  • 2
  • 8
  • 19

1 Answers1

2

If you have a custom QGraphcisItem you're in charge of the painting as well as the geometry. So you may paint the rectangle as top left (-300,-300) and bottom right (300,300) as long as you make sure you return a matching bounding rectangle by overriding and implementing QGraphicsItem::boundingRect().

Here's an example from the Qt documentation:

 class SimpleItem : public QGraphicsItem
 {
 public:
     QRectF boundingRect() const
     {
         qreal penWidth = 1;
         return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                       20 + penWidth, 20 + penWidth);
     }

     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                QWidget *widget)
     {
         painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
     }
 };
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61