Drawing QRubberBands with variabe size, but fixed aspect ratio: how to do it?
Solved
General and Desktop
-
Hi, I am using the QRubberBand class to draw a rectangular shape on the screen.
By trapping the mouse position when the mouse is pressed, and connecting mouse move events to the setGeometry function I can move the mouse around, with the rubberband following the mouse cursor in a rectangular shape. This all works ok.
I would like to constrain the mouse movements such that the rectangular rubber band still is of variable size, but always has a fixed aspect ratio. This could be handy, for instance, when cropping an image.
Are there any suggestions how I could achieve such a constraint?Kind Regards,
Bertwim -
@BwvB You could calculate the size so that it has the aspect ratio you want in mouseMoveEvent (see https://doc.qt.io/qt-6/qrubberband.html):
void Widget::mouseMoveEvent(QMouseEvent *event) { // Here calculate the QRect the way you need it rubberBand->setGeometry(QRect(origin, event->pos()).normalized()); }
-