Making movable QRubberBand be only movable within parent widget
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 :
//just testing if I move the rectangle of screen completely to the left where x < 0
QPoint newPos = m_rubberBand->geometry().topLeft();
QRect viewRect = viewport()->geometry();
newPos.setX(qMin(viewRect.right(), qMax(newPos.x(), viewRect.left()))); // w and h are the current width and height which I have once a rectangle is created m_rubberBand->setGeometry(newPos.x(), newPos.y(), w, h);
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!