调整大小后 QRubberBand 在 QGraphicsView 上移动

QRubberBand move on QGraphicsView after resizing

我在本主题中遇到了同样的问题:,经过几次尝试后,我意识到本主题的解决方案不适用于 QGraphics View。当我调整 window.

大小时,为什么我的选择围绕 QgraphicsView 移动
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# from PyQt4 import QtCore, QtWidgets

class ResizableRubberBand(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(ResizableRubberBand, self).__init__(parent)
        self.draggable = False
        self.mousePressPos = None
        self.mouseMovePos = None
        self._band = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
        self._band.setGeometry(550, 550, 550, 550)
        self._band.show()
        self.show()

    def mousePressEvent(self, event):
            if event.button() == QtCore.Qt.RightButton:
                self.mousePressPos = event.globalPos()                # global
                self.mouseMovePos = event.globalPos() - self.pos()    # local
                self.draggable = True
            elif event.button() == QtCore.Qt.LeftButton:
                self.position = QtCore.QPoint(event.pos())
                self.upper_left = self.position
                self.lower_right = self.position
                self.mode = "drag_lower_right"
                self._band.show()

    def mouseMoveEvent(self, event):
        if self.draggable and event.buttons() & QtCore.Qt.RightButton:
            globalPos = event.globalPos()
            print(globalPos)
            diff = globalPos - self.mouseMovePos
            self.move(diff)
            self.mouseMovePos = globalPos - self.pos()
        elif self._band.isVisible():
            # visible selection
            if self.mode is "drag_lower_right":
                self.lower_right = QtCore.QPoint(event.pos())
                # print(str(self.lower_right))
            elif self.mode is "drag_upper_left":
                self.upper_left = QtCore.QPoint(event.pos())
                # print(str(self.upper_left))
            # update geometry
            self._band.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())

    def mouseReleaseEvent(self, event):
        self.draggable = False

我的主要class:

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.band = ResizableRubberBand()
        scene = QtWidgets.QGraphicsScene(self)
        photo = QtGui.QPixmap('image.jpg')
        scene.addPixmap(photo)
        self.band.setScene(scene)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.band)



if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(800, 100, 600, 500)
    window.show()
    sys.exit(app.exec_())

调整大小前:

调整大小后:

问题是因为图片的坐标系和QRubberBand的坐标系不一样。所以解决方案是你们共享相同的坐标系,为此我们将 QRubberBand 添加到场景中。

from PyQt5 import QtCore, QtGui, QtWidgets


class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(GraphicsView, self).__init__(parent)
        self.setScene(QtWidgets.QGraphicsScene(self))
        self.m_rubberBand = QtWidgets.QRubberBand(
            QtWidgets.QRubberBand.Rectangle
        )
        self.m_rubberBand.setGeometry(QtCore.QRect(-1, -1, 2, 2))
        self.m_rubberBand.hide()
        item = self.scene().addWidget(self.m_rubberBand)
        item.setZValue(1)
        self.m_draggable = False

        self.m_origin = QtCore.QPoint()

    def mousePressEvent(self, event):
        self.m_origin = self.mapToScene(event.pos()).toPoint()

        self.m_rubberBand.setGeometry(
            QtCore.QRect(self.m_origin, QtCore.QSize())
        )
        self.m_rubberBand.show()

        self.m_draggable = True
        super(GraphicsView, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if self.m_draggable:
            end_pos = self.mapToScene(event.pos()).toPoint()
            self.m_rubberBand.setGeometry(
                QtCore.QRect(self.m_origin, end_pos).normalized()
            )
            self.m_rubberBand.show()

    def mouseReleaseEvent(self, event):
        end_pos = self.mapToScene(event.pos()).toPoint()
        self.m_rubberBand.setGeometry(
            QtCore.QRect(self.m_origin, end_pos).normalized()
        )
        self.m_draggable = False


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = GraphicsView()
    photo = QtGui.QPixmap("image.jpg")
    w.scene().addPixmap(photo)
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())