View Full Version : Subclassing QGraphicsRectItem to have a reference rectangle on an image

18th November 2011, 17:05
In my application I am displaying an image on the MainWindow and also the same exact image in a smaller widget just below. For this I have a QGraphicsView, QGraphicsScene and a QGraphicsItem to display the image. And there are zooming capabilities as well.

Now what I am trying to do is on the small image below, which never changes, I created a QGraphicsRectItem so that when I zoom in the big main image, the rectangle item that is displayed only on the small image, to be as a reference of how much I am zoomed and which portion of the original image I am looking at. Also I want to be able to move this rectangle and have it scroll the main image when I move it.

The problems I am having is making the QGraphicsRectItem on the scene behave as I want it. For this I have a subclass of QGraphicsRectItem and that item is added to the scene so it is displaying the rectangle on top of the small image fine. I managed to get the events when I press, move and release on the small view, but those events are entered no matter where in the small image I press or move, etc. and I need it to only respond when the press and move are in the Rectangle (when my intention is to move the rectangle).

For this I tried implementing the HoverEnter and HoverLeave events on the Rect Item but they are never reached. Also to resize the rectangle when I zoom in I tried with setRect and it doesn’t do anything, the rectangle stays the same size on the scene. Can anyone please help me I would really appreciate any ideas, or tips on what might be wrong.


18th November 2011, 17:21
Did you set the GraphicsItemFlags on the reference rectangle so it can be movable, selectable, focusable, etc? Otherwise, if nothing in the scene is allowed to respond to mouse events, your item won't see them.

18th November 2011, 17:34
Yes I did! I set my flags like this:

setFlags(QGraphicsRectItem::ItemIsMovable | QGraphicsRectItem::ItemIsSelectable |


After doing this I am having problems..