Subclassing QGraphicsRectItem to have a reference rectangle on an image
sergex last edited by
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.
rokemoon last edited by
[quote author="sergex" date="1321629952"]
For this I tried implementing the HoverEnter and HoverLeave events on the Rect Item but they are never reached. [/quote]
To have hover events you need to set this "setAcceptHoverEvents":http://doc.qt.nokia.com/latest/qgraphicsitem.html#setAcceptHoverEvents
sergex last edited by
Yes, I did set that and did not make any difference.
I also set the flags in my GraphicsRectItem class to be :
setFlags(QGraphicsRectItem::ItemIsMovable | QGraphicsRectItem::ItemIsSelectable |
My main problem of at least changing the size of the rectangular item remains. For some reason setRect does not do anything.
I have in the paint event of my rect item this:
void MyRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
pen.setColor(QColor(200, 200, 200));
QBrush brush(Qt::Dense5Pattern); brush.setColor(QColor(200, 200, 200)); painter->setPen(pen); painter->setBrush(brush); painter->drawRect(boundingRect());
And the bounding rect method I did this:
QRectF MyRect::boundingRect() const
QRectF rect(leftPos, 0, rightPos - leftPos, myScene->sceneRect().height());
In a different method is where I set the leftPos and rightPos values and do setRect(boundingRect()); I tried before calling this to do prepareGeometryChange() but still no effect. Just for testing I did in the same method setRect(x,y,w,h) instead and gave random values for x,y,w,h and the rectangle doesn't change.
Any ideas? I would really appreciate it!