我正在尝试使用Qgraphicview裁剪图像 但我面临一个抵消问题 这是QMainWindow
class ImageViewer(QtWidgets.QMainWindow):
def __init__(self):
super(ImageViewer, self).__init__()
self.scene = QtWidgets.QGraphicsScene()
self.imageLabel = QtWidgets.QLabel()
self.cropLabel = QtWidgets.QLabel()
self.scrollArea = QtWidgets.QScrollArea()
self.scrollArea.setWidget(self.imageLabel)
self.setCentralWidget(widget)
self.scene.addWidget(splitter)
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setMouseTracking(True)
self.view.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
...
def mousePressEvent(self, eventQMouseEvent):
print("pressed")
self.originQPoint = self.view.mapToScene(eventQMouseEvent.pos())
self.currentQRubberBand = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, QtCore.QSize()))
self.currentQRubberBand.show()
def mouseMoveEvent(self, eventQMouseEvent):
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())
def mouseReleaseEvent (self, eventQMouseEvent):
#self.currentQRubberBand.hide()
currentQRect = self.currentQRubberBand.geometry()
print(currentQRect)
self.currentQRubberBand.deleteLater()
cropPixmap = self.pixmap.copy(currentQRect)
self.cropLabel.setPixmap(cropPixmap)
cropPixmap.save('output.png')
代码效果很好,但我有一个偏移量(似乎是边缘或菜单栏值或混合)。所以我想我不是在良好的系统中,但不知道如何访问它
答案 0 :(得分:0)
最后,我离开了QGraphicView并在处理QLabel的专用Widget类中重新实现了鼠标事件。 我工作得很好
class View(QtWidgets.QLabel):
def __init__(self):
super(View,self).__init__()
self.cropLabel = QtWidgets.QLabel(self)
self.label = QtWidgets.QLabel(self)
self.ogpixmap = QtGui.QPixmap()
fileName = r'C:/Users/user11.HPO-SAMAT/Pictures/Lake.jpg'
image = QtGui.QImage(fileName)
self.pixmap = QtGui.QPixmap.fromImage(image)
self.label.setPixmap(self.pixmap)
self.label.adjustSize()
def mousePressEvent(self, event):
self.originQPoint = event.pos()
self.currentQRubberBand = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, QtCore.QSize()))
self.currentQRubberBand.show()
def mouseMoveEvent(self, event):
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, event.pos()).normalized())
def mouseReleaseEvent (self, event):
self.currentQRubberBand.hide()
currentQRect = self.currentQRubberBand.geometry()
self.currentQRubberBand.deleteLater()
cropPixmap = self.pixmap.copy(currentQRect)
self.cropLabel.setPixmap(cropPixmap)
class Viewer(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.view = View()
self.setCentralWidget(self.view)