如何防止在特定区域夹住 QRubberband?

How to prevent clip a QRubberband in a certain region?

我打算制作一个小型照片裁剪软件,我遇到了这个问题,当我移动我制作的 QRubberband 时,它有点移动到 QLabel 的边界。

示例代码如下:(左键单击并拖动以创建 QRubberband 并右键单击以移动 QRubberband

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class ResizableRubberBand(QWidget):
    def __init__(self, parent=None):
        super(ResizableRubberBand, self).__init__(parent)

        self.draggable = True
        self.dragging_threshold = 5
        self.mousePressPos = None
        self.mouseMovePos = None
        self.borderRadius = 5

        self.setWindowFlags(Qt.SubWindow)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(
            QSizeGrip(self), 0,
            Qt.AlignLeft | Qt.AlignTop)
        layout.addWidget(
            QSizeGrip(self), 0,
            Qt.AlignRight | Qt.AlignBottom)
        self._band = QRubberBand(
            QRubberBand.Rectangle, self)
        self._band.show()
        self.show()

    def resizeEvent(self, event):
        self._band.resize(self.size())

    def paintEvent(self, event):
        # Get current window size
        window_size = self.size()
        qp = QPainter()
        qp.begin(self)
        qp.setRenderHint(QPainter.Antialiasing, True)
        qp.drawRoundedRect(0, 0, window_size.width(), window_size.height(),
                        self.borderRadius, self.borderRadius)
        qp.end()

    def mousePressEvent(self, event):
        if self.draggable and event.button() == Qt.RightButton:
            self.mousePressPos = event.globalPos()                # global
            self.mouseMovePos = event.globalPos() - self.pos()    # local
        super(ResizableRubberBand, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if self.draggable and event.buttons() & Qt.RightButton:
            globalPos = event.globalPos()
            moved = globalPos - self.mousePressPos
            if moved.manhattanLength() > self.dragging_threshold:
                # Move when user drag window more than dragging_threshold
                diff = globalPos - self.mouseMovePos
                self.move(diff)
                self.mouseMovePos = globalPos - self.pos()
        super(ResizableRubberBand, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.mousePressPos is not None:
            if event.button() == Qt.RightButton:
                moved = event.globalPos() - self.mousePressPos
                if moved.manhattanLength() > self.dragging_threshold:
                    # Do not call click event or so on
                    event.ignore()
                self.mousePressPos = None
        super(ResizableRubberBand, self).mouseReleaseEvent(event)

class mQLabel(QLabel):
    def __init__(self, parent=None):
        QLabel.__init__(self, parent)
        self.setContentsMargins(0,0,0,0)
        self.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton: #and (hasattr(self, 'bla')):
            self.first_mouse_location = (event.x(), event.y())
            self.band = ResizableRubberBand(self)
            self.band.setGeometry(event.x(), event.y(), 0, 0)

    def mouseMoveEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            first_mouse_location_x = self.first_mouse_location[0]
            first_mouse_location_y = self.first_mouse_location[1]
            new_x, new_y = event.x(), event.y()
            difference_x = new_x - first_mouse_location_x
            difference_y = new_y - first_mouse_location_y
            self.band.resize(difference_x, difference_y)

class App(QWidget):

    def __init__(self):
        super().__init__()

        ## Set main window attributes
        self.setFixedSize(1000,600)

        # Add Label
        self.label = mQLabel()
        self.label.setStyleSheet("border: 1px solid black;")
        self.label_layout = QHBoxLayout(self)
        self.label_layout.addWidget(self.label)

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

我真的想不出一个可以做到的简单解决方案。我的第一直觉是获取父级的大小,但我不知道下一步该怎么做。

您需要根据父 rectangle.geometry 比较 widget geometry

请注意,您不应使用全局位置,因为跟踪应始终发生在局部坐标(相对于父坐标)中。此外,一旦开始拖动,您就不需要再次检查 manhattanLength(),并且 self.mousePressPos 在任何情况下都应该在发布时被清除。

class ResizableRubberBand(QRubberBand):
    is_dragging = False
    def mousePressEvent(self, event):
        if self.draggable and event.button() == Qt.RightButton:
            self.mousePressPos = event.pos()
        super(ResizableRubberBand, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if self.draggable and event.buttons() & Qt.RightButton:
            diff = event.pos() - self.mousePressPos
            if not self.is_dragging:
                if diff.manhattanLength() > self.dragging_threshold:
                    self.is_dragging = True
            if self.is_dragging:
                geo = self.geometry()
                parentRect = self.parent().rect()
                geo.translate(diff)
                if not parentRect.contains(geo):
                    if geo.right() > parentRect.right():
                        geo.moveRight(parentRect.right())
                    elif geo.x() < parentRect.x():
                        geo.moveLeft(parentRect.x())
                    if geo.bottom() > parentRect.bottom():
                        geo.moveBottom(parentRect.bottom())
                    elif geo.y() < parentRect.y():
                        geo.moveTop(parentRect.y())
                self.move(geo.topLeft())
                self.clearMask()
        super(ResizableRubberBand, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.mousePressPos is not None:
            if event.button() == Qt.RightButton and self.is_dragging:
                event.ignore()
                self.is_dragging = False
            self.mousePressPos = None
        super(ResizableRubberBand, self).mouseReleaseEvent(event)

另请注意,您实际上并没有以非常好的方式使用 QRubberBand,这也是因为您正在绘制其边界。无论如何,更好的实现是直接继承 QRubberBand:

class ResizableRubberBand(QRubberBand):
    def __init__(self, parent=None):
        super(ResizableRubberBand, self).__init__(QRubberBand.Rectangle, parent)
        self.setAttribute(Qt.WA_TransparentForMouseEvents, False)

        self.draggable = True
        self.is_dragging = False
        self.dragging_threshold = 5
        self.mousePressPos = None
        self.borderRadius = 5

        self.setWindowFlags(Qt.SubWindow)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(
            QSizeGrip(self), 0,
            Qt.AlignLeft | Qt.AlignTop)
        layout.addWidget(
            QSizeGrip(self), 0,
            Qt.AlignRight | Qt.AlignBottom)
        self.show()

    def resizeEvent(self, event):
        self.clearMask()

    def paintEvent(self, event):
        super().paintEvent(event)
        qp = QPainter(self)
        qp.setRenderHint(QPainter.Antialiasing)
        qp.translate(.5, .5)
        qp.drawRoundedRect(self.rect().adjusted(0, 0, -1, -1), 
            self.borderRadius, self.borderRadius)

如果你只是为了美观而使用 QRubberBand,你根本不需要它,你可以从 QWidget 继承并使用样式函数来绘制一个“假”橡皮筋:

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setRenderHint(QPainter.Antialiasing)
        qp.translate(.5, .5)
        opt = QStyleOptionRubberBand()
        opt.initFrom(self)
        style = self.style()
        style.drawControl(style.CE_RubberBand, opt, qp)
        qp.drawRoundedRect(self.rect().adjusted(0, 0, -1, -1), 
            self.borderRadius, self.borderRadius)