我有一个可调整大小的橡皮筋,带有拖动手柄,这很有趣。我想将其强制为 4x3 选择矩形。它可以工作,但是有剪裁伪影,并且把手不会留在角落里。
代码:
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))