In rubber band rectangle zoom in, rectangle is not visible.
-
I am using rubber band rectangle zoom in.
There are 2 cases for rubber band rectangle creation according to point selection ( rectangles start point ( top left ) and end point ( right bottom)- Starting point of rectangle is on the blank space
- Starting point is on QGraphicsItem.
Above diagram shows, 2nd case. Starting point (point A) is inside QGraphicsItem.
And in this case I am not able to see rubber band rectangle. But it gives proper output. ( it zoom in that particular area)Here is my code:
bool myViewer::eventFilter(QObject* watched, QEvent* event) { bool filterEvent = false; switch (event->type()) { case QEvent::MouseButtonPress: { QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); _rubberBandOrigin = mouseEvent->pos(); _rubberBandActive = true; break; } case QEvent::MouseButtonRelease: { if (_rubberBandActive) { QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); _rubberBandEnd = mouseEvent->pos(); QGraphicsView* view = static_cast<QGraphicsView*>(watched->parent()); QRectF zoomRectInScene = QRectF(view->mapToScene(_rubberBandOrigin), view->mapToScene(_rubberBandEnd)); view->fitInView(zoomRectInScene, Qt::KeepAspectRatio); _rubberBandActive = false; } break;
Issue :
I want to see rubber band rectangle in above case also.
P.S. In above case, I get correct output ( i.e. area gets zoomed in ) but not able to see rubber band rectangle.If I removed yellow color of rectangle, still I can not see rubber band rectangle.
-
@SGaist After searching on internet, I found reason for this.
When I try to create rubber band rectangle on blank space, it gets created on scene, so paint() gets called and we can see rubber band rectangle.But when we try to create rubber band rectangle on QGraphicsRectItem, it is not creating on scene but creating on rectangle. So paint does not get called.
So I tried 1 solution :
When I will create top left corner of rectangle, I am getting that point and checking under that point is there any object.
If it has any object under it, then I am changing it's opacity by setOpacity(0.0) and when I release mouse ( i.e. creating bottom right points of rectangle) I am again setting it's opacity to normal.
And in this , I can see rubber band rectangle has been created.
But this way is not good, because for some time, due to low opacity, rectangle's border gets disappear, which I dont want.Is there any alternative ?
-
Can you show exactly how you manage your rubber band ?
Are you other items movable ? -
@SGaist I have given you all the code related to rubber band rectangle.
rubberBandOrigin ( from event mouse button pressed ) and rubberBandEnd ( from event mouse button released) are rectangles top left and bottom right points.And
QRectF zoomRectInScene = QRectF(view->mapToScene(_rubberBandOrigin), view->mapToScene(_rubberBandEnd)); view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
draws rubber band rectangle.
-
Ok, is see the issue: I took you use the QRubberBand class for your purpose.
Based on what you want to do, it seems it would be simpler. The rubber band being independent of the view will also not be dependent of if for its rendering.