0

我只是好奇我是否可以制作一个嵌套的QRubberband. (我或某人可能会发现它的用途)。我设法从这个答案编辑代码以制作嵌套的QRubberband. 一切都很好并且可以正常工作,直到我将其移入QRubberband其 parent QRubberband。我很困惑,因为当我拖动它时它会疯狂地移动。

这是活动的简短 gif

这是示例代码:

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

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

        self.draggable = True
        self.dragging = False
        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)

    def mousePressEvent(self, event):
        if self.draggable and event.button() == Qt.RightButton:
            self.mousePressPos = event.pos()

        if event.button() == Qt.LeftButton:
            self.first_mouse_location = (event.x(), event.y())
            self.band = ResizableRubberBand(self)
            self.band.setGeometry(event.x(), event.y(), 0, 0)
        
        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.dragging:
                if diff.manhattanLength() > self.dragging_threshold:
                    self.dragging = True
            if self.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())

        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)
        super(ResizableRubberBand, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.mousePressPos is not None:
            if event.button() == Qt.RightButton and self.dragging:
                event.ignore()
                self.dragging = False
            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:
            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_())

我试图弄清楚 2 个小时,但我似乎无法弄清楚是什么导致了不必要的运动。我最好的猜测是它来自于,mouseMoveEvent但我不太确定它是来自父母QRubberband还是来自QRubberband内部。我希望有人能弄清楚这里发生了什么。

4

1 回答 1

1

问题是对鼠标事件的基本实现的调用,默认情况下,它会传播到不直接实现它们的小部件的父级,包括 QrubberBand,它通常根本不拦截鼠标事件我们通过禁用相对窗口属性)。

由于父对象本身是一根橡皮筋,它自己也会被移动,从而使子对象的移动递归,因为它接收到鼠标移动正是由于它被移动了:请记住,如果一个小部件被移动并且鼠标不直接跟随相同的移动,它可能会收到相对于其新位置的鼠标移动事件。

您可以return在处理它之前调用它,或者根本不调用它,这取决于您的需要。

重要的是它是一致的(特别是对于按下和移动),否则小部件可能会在没有接收到鼠标按下的情况下接收鼠标移动,这将因为尚未设置变量而崩溃。

请注意,如果您正在为剪辑/选择、绘图等制作更高级的编辑器,您应该真正考虑使用图形视图框架:虽然更复杂且学习曲线更陡峭,但您将很快就会发现,继续开发基本的 QWidgets 会变得越来越复杂和困难,以至于很难解决问题,特别是如果您要处理图像缩放甚至基本的滚动和缩放。
QWidget 和 QLabel 实现不适用于图像管理,甚至不是简单的编辑,而且自定义放置/绘制/嵌套的小部件通常难以处理。考虑到在图形场景中使用类似的选择工具会容易得多:例如,移动实现几乎完全没有必要,因为设置一个简单的标志就足以使项目可移动。

于 2022-01-02T08:43:36.003 回答