我有一个QGraphicsView,其宽度和长度与我要保存的像素图不同。保存像素图时,我希望它处于全分辨率。
def save(self):
painter = QtGui.QPainter()
painter.setViewport(rect.toRect())
pixmap = QtGui.QPixmap().grabWidget(self)
pixmap_scaled = pixmap.scaled(1275, 2004, QtCore.Qt.KeepAspectRatio)
pixmap_scaled.save('sample.png')
执行此操作时,像素图未处于完整分辨率。
下面的代码是高分辨率图像的fitinview代码,以适合qgraphicsview
def fitInView(self, scale=True):
rect = QtCore.QRectF(self._photo.pixmap().rect())
if not rect.isNull():
self.setSceneRect(rect)
if self.hasPhoto():
unity = self.transform().mapRect(QtCore.QRectF(0, 0, 1, 1))
self.scale(1 / unity.width(), 1 / unity.height())
viewrect = self.viewport().rect()
scenerect = self.transform().mapRect(rect)
factor = min(viewrect.width() / scenerect.width(),
viewrect.height() / scenerect.height())
self.scale(factor, factor)
self._zoom = 0
我的QGraphicsView
class FrontGraphics(QtGui.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
def __init__(self, parent):
super(FrontGraphics, self).__init__(parent)
self._zoom = 0
self._empty = True
self.setMouseTracking(True)
self._scene = QtGui.QGraphicsScene(self)
self._photo = QtGui.QGraphicsPixmapItem()
self._scene.addItem(self._photo)
self.setScene(self._scene)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
# self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
self.setFrameShape(QtGui.QFrame.NoFrame)
# self.setFixedSize(350, 600)
self._idpicture = IDPicture(QtCore.QPointF(50, 50))
self._scene.addItem(self._idpicture)
self._studentid = StudentId()
self._scene.addItem(self._studentid)
self._fullname = FullName()
self._scene.addItem(self._fullname)
self._section = Section()
self._scene.addItem(self._section)
我的QWidget
class FrontWindow(QtGui.QWidget):
def __init__(self):
super(FrontWindow, self).__init__()
self.viewer = FrontGraphics(self)
self.btnLoad = QtGui.QToolButton(self)
self.btnLoad.setText('Load image')
self.btnLoad.clicked.connect(self.loadImage)
self.btnClear = QtGui.QToolButton(self)
self.btnClear.setText('Clear')
self.btnClear.clicked.connect(self.clearImage)
self.btnPixInfo = QtGui.QLabel(self)
self.setFixedWidth(500)
self.setFixedHeight(800)
self.setGeometry(500, 300, 500, 300)
# Arrange layout
VBlayout = QtGui.QVBoxLayout(self)
VBlayout.addWidget(self.viewer)
HBlayout = QtGui.QHBoxLayout()
HBlayout.setAlignment(QtCore.Qt.AlignLeft)
# HBlayout.addWidget(self.btnLoad)
HBlayout.addWidget(self.btnClear)
HBlayout.addWidget(self.btnPixInfo)
VBlayout.addLayout(HBlayout)
我在这里Grabwidget to grab the complete view even if it is zoomed in尝试了答案,但无法做到