无法停止在qgraphicsview中绘制矩形

时间:2019-08-26 18:19:40

标签: python pyqt5 rectangles

在我的程序中,我可以选择一个设置self.rectmode = 1的按钮。一旦将该变量设置为1,它将使用鼠标事件在qgraphicsview上绘制矩形。按下按钮设置self.rectmode = 0后,程序将继续使用鼠标事件绘制矩形。我是否缺少一些线条来结束矩形绘制事件。我的代码如下,谢谢:

def mousePressEvent(self, event):

    if (self.rectmode==1 and event.button() == Qt.LeftButton and not  self._photo.pixmap().isNull()):
        self.origin = event.pos()
        self.rubberBand.setGeometry(QRect(self.origin, QSize()))
        self.rectChanged.emit(self.rubberBand.geometry())
        self.rubberBand.show()
        self.changeRubberBand = True

     else:
        super(PhotoViewer, self).mousePressEvent(event)

def mouseMoveEvent(self, event):

    if self.rectmode==1 and self.changeRubberBand:
        self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized())
        self.rectChanged.emit(self.rubberBand.geometry())

    else:
        super(PhotoViewer, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):

    if self.rectmode==1 and event.button() == Qt.LeftButton:
        self.changeRubberBand = False
        self.endpoint = event.pos()
        print(self.origin.x())
        print(self.origin.y())
        print(self.endpoint.x())
        print(self.endpoint.y())

    else:
        super(PhotoViewer, self).mouseReleaseEvent(event)

1 个答案:

答案 0 :(得分:0)

在您的代码rectmode中始终为1,我认为这就是问题所在,下面是一个有效的示例,我也删除了变量changeRubberBand,因为仅使用变量就可以实现相同的目的rectMode

import sys

from PyQt5.Qt import QApplication, QRect, QSize, Qt, QRubberBand, QVBoxLayout, pyqtSignal
from PyQt5.QtWidgets import QMainWindow


class PhotoViewer(QMainWindow):
    rectChanged = pyqtSignal(QRect)

    def __init__(self):
        super().__init__()
        self.origin = None
        self.endpoint = None
        self.rectMode = 0

        self.setFixedSize(1024, 768)
        self.layout = QVBoxLayout(self)

        self.rubberBand = QRubberBand(QRubberBand.Rectangle, self)
        self.rubberBand.hide()
        self.layout.addChildWidget(self.rubberBand)

    def mousePressEvent(self, event):
        if self.rectMode == 0 and event.button() == Qt.LeftButton:
            self.origin = event.pos()
            self.rubberBand.setGeometry(QRect(self.origin, QSize()))
            self.rectChanged.emit(self.rubberBand.geometry())
            self.rubberBand.show()
            self.rectMode = 1
        else:
            super(PhotoViewer, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if self.rectMode == 1:
            self.rubberBand.setGeometry(QRect(self.origin, event.pos()).normalized())
            self.rectChanged.emit(self.rubberBand.geometry())

        else:
            super(PhotoViewer, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.rectMode == 1 and event.button() == Qt.LeftButton:
            self.rectMode = 0
            self.endpoint = event.pos()
            print(self.origin.x())
            print(self.origin.y())
            print(self.endpoint.x())
            print(self.endpoint.y())

        else:
            super(PhotoViewer, self).mouseReleaseEvent(event)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = PhotoViewer()
    mainWindow.show()
    sys.exit(app.exec_())

希望有帮助。