无法停止在 qgraphicsview 中绘制矩形
Can't stop drawing rectangles in qgraphicsview
在我的程序中,我可以 select 一个设置 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)
在您的代码中 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_())
希望对您有所帮助。
在我的程序中,我可以 select 一个设置 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)
在您的代码中 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_())
希望对您有所帮助。