Hello,

I have a rectangular QRubberBand that its size is made with the mouse clicking and dragging. Once the mouse is released the rectangle stays on screen and if you click and drag the rectangle you begin moving it. It all works fine except that I am trying to restrict the movement to only be within parent widget boundaries, because it can move out of the screen right now.

Is there something similar to QGraphicsItem::itemChange(..) for a QWidget based widget? I think there I could maintain the rectangle within bounds every time it gets out with the ItemPositionChange flag.. I am looking for a similar way of handling this case witha QRubberband.

So far tried in the mouseMoveEvent of my parent widget to do :

Qt Code:
  1. if(viewport()->geometry().contains(m_rubberBand->geometry(), true))
  2. moveRectangle(event);
  3. else
  4. {
  5. //just testing if I move the rectangle of screen completely to the left where x < 0
  6. QPoint newPos = m_rubberBand->geometry().topLeft();
  7. QRect viewRect = viewport()->geometry();
  8.  
  9. newPos.setX(qMin(viewRect.right(), qMax(newPos.x(), viewRect.left())));
  10. // w and h are the current width and height which I have once a rectangle is created
  11. m_rubberBand->setGeometry(newPos.x(), newPos.y(), w, h);
  12. }
To copy to clipboard, switch view to plain text mode 

What this does so far it avoids moving the rectangle out of the parent widget bounds but it gets stuck to the edge and its not possible to move anymore. Any help greatly appreciated!

Thanks.