I have a resizable rubber-band with drag handles that is acting funny. I want to force it to a 4x3 selection rectangle. It works, but there are clipping artefacts, and the grips don't stay in the corners.
Code:
class Resizable_rubber_band(QWidget):
def __init__(self, parent=None):
super(Resizable_rubber_band, self).__init__(parent)
self.setWindowFlags(Qt.SubWindow)
self.layout = QHBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.grip1 = QSizeGrip(self)
self.grip2 = QSizeGrip(self)
self.layout.addWidget(self.grip1, 0, Qt.AlignLeft | Qt.AlignTop)
self.layout.addWidget(self.grip2, 0, Qt.AlignRight | Qt.AlignBottom)
self.rubberband = QRubberBand(QRubberBand.Rectangle, self)
self.rubberband.move(0, 0)
self.rubberband.show()
self.show()
def resizeEvent(self, event):
force 4h x 3w aspect ratio using QSize
if width < height: height = 1.333 * width
if width > height: width = height / 1.333
width = self.width()
height = self.height()
if(width < height):
height = int(width * 1.333)
else:
width = int(height / 1.333)
self.rubberband.resize(QSize(width, height))
You need to scale and resize both the rubber-band and its container.
Here is a demo based on your example: