Вы должны использовать метод fitInView()
QGraphicsView в методе resizeEvent()
QGraphicsView:
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.setScene(scene)
self.m_pixmap_item = self.scene().addPixmap(QtGui.QPixmap())
def setPixmap(self, pixmap):
self.m_pixmap_item.setPixmap(pixmap)
def resizeEvent(self, event):
if not self.m_pixmap_item.pixmap().isNull():
self.fitInView(self.m_pixmap_item, QtCore.Qt.KeepAspectRatio)
super(GraphicsView, self).resizeEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = GraphicsView()
photo = QtGui.QPixmap("image.jpg")
w.setPixmap(photo)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())