【问题标题】:Forcing QRubberBand to a specific aspect ratio强制 QRubberBand 到特定的纵横比
【发布时间】:2018-06-11 19:24:51
【问题描述】:

我有一个可调整大小的橡皮筋,带有拖动手柄,这很有趣。我想将其强制为 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))

【问题讨论】:

    标签: python qt pyqt resize rubber-band


    【解决方案1】:

    您需要对橡皮筋及其容器进行缩放和调整大小。

    这是基于您的示例的演示:

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class ResizableRubberBand(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(ResizableRubberBand, self).__init__(parent)
            self.setWindowFlags(QtCore.Qt.SubWindow)
            layout = QtWidgets.QHBoxLayout(self)
            layout.setContentsMargins(0, 0, 0, 0)
            layout.addWidget(
                QtWidgets.QSizeGrip(self), 0,
                QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
            layout.addWidget(
                QtWidgets.QSizeGrip(self), 0,
                QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
            self._band = QtWidgets.QRubberBand(
                QtWidgets.QRubberBand.Rectangle, self)
            self._band.show()
            self.show()
    
        def resizeEvent(self, event):
            size = QtCore.QSize(3, 4)
            size.scale(self.size(), QtCore.Qt.KeepAspectRatio)
            self.resize(size)
            self._band.resize(self.size())
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.button = QtWidgets.QPushButton('Show Rubber Band')
            self.button.clicked.connect(self.handleButton)
            self.label = QtWidgets.QLabel()
            self.label.setScaledContents(True)
            self.label.setPixmap(QtGui.QPixmap('image.jpg'))
            layout = QtWidgets.QVBoxLayout(self)
            layout.addWidget(self.label)
            layout.addWidget(self.button)
    
        def handleButton(self):
            self.band = ResizableRubberBand(self.label)
            self.band.setGeometry(50, 50, 150, 300)
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.setGeometry(800, 100, 600, 500)
        window.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 使用比例和 KeepAspecRatio 是一种非常好的方法。完全没有想到 QWidget.scale 函数只是在我的像素图方面看到它......谢谢!现在我只需要实现鼠标中心拖动,但这只是项目工作后的结冰。
    猜你喜欢
    • 2013-03-06
    • 2017-03-28
    • 2012-06-24
    • 2011-06-23
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 2021-11-14
    相关资源
    最近更新 更多