我正在用python PyQt4构建图像编辑器。我已经声明了一个QGraphicsView类,其中包含QGraphicsScene来显示图像。我单击图像时需要图像的坐标,而不是QGraphicsView的坐标。我该如何实现。我附上我的QGraphicsView类以供参考。
class MyView(QtGui.QGraphicsView):
rectChanged = pyqtSignal(QtCore.QRect)
def __init__(self):
super(MyView, self).__init__()
#QtGui.QGraphicsView.__init__(self)
self.setGeometry(20, 30, 100, 200)
self.scene = QtGui.QGraphicsScene(self)
self.resize(1326, 400)
self.setScene(self.scene)
self._pixmapHandle = None
self.x1=0
self.y1=0
self.x2=0
self.y2=0
self.choice=2
self.rubberBand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
self.setMouseTracking(True)
self.origin = QtCore.QPoint()
self.changeRubberBand = False
self.rubberBand.move(0, 0)
# Image aspect ratio mode.
# !!! ONLY applies to full image. Aspect ratio is always ignored when zooming.
# Qt.IgnoreAspectRatio: Scale image to fit viewport.
# Qt.KeepAspectRatio: Scale image to fit inside viewport, preserving aspect ratio.
# Qt.KeepAspectRatioByExpanding: Scale image to fill the viewport, preserving aspect ratio.
self.aspectRatioMode = Qt.KeepAspectRatio
# Scroll bar behaviour.
# Qt.ScrollBarAlwaysOff: Never shows a scroll bar.
# Qt.ScrollBarAlwaysOn: Always shows a scroll bar.
# Qt.ScrollBarAsNeeded: Shows a scroll bar only when zoomed.
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# Stack of QRectF zoom boxes in scene coordinates.
self.zoomStack = []
# Flags for enabling/disabling mouse interaction.
self.canZoom = True
self.canPan = True
def save(Self, filename, imgarray):
if (len(imgarray.shape))==3:
im=Image.fromarray(imgarray, 'RGB')
else:
im=Image.fromarray(imgarray)
im.save(filename)
def resizeEvent(self, event):
""" Maintain current zoom on resize.
"""
self.rubberBand.resize(self.size())
def mousePressEvent(self, event):
""" Start mouse pan or zoom mode.
"""
self.x1=event.x()
self.y1=event.y()
print(self.x1)
print(self.y1)
if event.button() == Qt.LeftButton:
if self.canPan:
self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
elif event.button() == Qt.RightButton and self.choice==0:
self.origin = event.pos()
self.rubberBand.setGeometry(QtCore.QRect(self.origin, QtCore.QSize()))
self.rectChanged.emit(self.rubberBand.geometry())
self.rubberBand.show()
self.changeRubberBand = True
elif event.button() == Qt.RightButton and self.choice==1:
self.origin = event.pos()
self.rubberBand.setGeometry(QtCore.QRect(self.origin, QtCore.QSize()))
self.rectChanged.emit(self.rubberBand.geometry())
self.rubberBand.show()
self.changeRubberBand = True
QtGui.QGraphicsView.mousePressEvent(self, event)
def mouseMoveEvent(self, event):
if self.changeRubberBand:
self.rubberBand.setGeometry(QtCore.QRect(self.origin, event.pos()).normalized())
self.rectChanged.emit(self.rubberBand.geometry())
QtGui.QGraphicsView.mouseMoveEvent(self, event)
def mouseReleaseEvent(self, event):
""" Stop mouse pan or zoom mode (apply zoom if valid).
"""
self.x2=event.x()
self.y2=event.y()
print(self.x2)
print(self.y2)
QtGui.QGraphicsView.mouseReleaseEvent(self, event)
scenePos = self.mapToScene(event.pos())
if event.button() == Qt.LeftButton:
self.setDragMode(QtGui.QGraphicsView.NoDrag)
elif event.button() == Qt.RightButton and self.choice==0:
global croppedFname
croppedFname=''
self.changeRubberBand = False
currentQRect = self.rubberBand.geometry()
outputimg=QtGui.QPixmap(self.x2-self.x1, self.y2-self.y1)
painter=QtGui.QPainter(outputimg)
self.render(painter, source=currentQRect, mode=QtCore.Qt.KeepAspectRatio)
outputimg.save('/home/sounak/Desktop/1.jpg')
painter.end()
if self.x1==self.x2 or self.y1==self.y2:
croppedFname=''
else:
croppedFname='/home/sounak/Desktop/1.jpg'
self.setDragMode(QtGui.QGraphicsView.NoDrag)
elif event.button() == Qt.RightButton and self.choice==1:
self.changeRubberBand = False
currentQRect = self.rubberBand.geometry()
self.setDragMode(QtGui.QGraphicsView.NoDrag)