Instead of using QLabel + QScrollArea where you have to implement many tasks such as zooming, you should use QGraphicsView that already implements those functionalities:
from PyQt5 import QtWidgets, QtGui, QtCore
class ImageViewer(QtWidgets.QGraphicsView):
factor = 2.0
def __init__(self, parent=None):
super().__init__(parent)
self.setRenderHints(
QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform
)
self.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
self.setBackgroundRole(QtGui.QPalette.Dark)
scene = QtWidgets.QGraphicsScene()
self.setScene(scene)
self._pixmap_item = QtWidgets.QGraphicsPixmapItem()
scene.addItem(self._pixmap_item)
def load_image(self, fileName):
pixmap = QtGui.QPixmap(fileName)
if pixmap.isNull():
return False
self._pixmap_item.setPixmap(pixmap)
return True
def zoomIn(self):
self.zoom(self.factor)
def zoomOut(self):
self.zoom(1 / self.factor)
def zoom(self, f):
self.scale(f, f)
def resetZoom(self):
self.resetTransform()
def fitToWindow(self):
self.fitInView(self.sceneRect(), QtCore.Qt.KeepAspectRatio)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.view = ImageViewer()
self.setCentralWidget(self.view)
self.createActions()
self.createMenus()
self.resize(640, 480)
def open(self):
image_formats = " ".join(
[
"*." + image_format.data().decode()
for image_format in QtGui.QImageReader.supportedImageFormats()
]
)
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
self,
self.tr("Open Image"),
QtCore.QDir.currentPath(),
self.tr("Image Files({})".format(image_formats)),
)
if fileName:
is_loaded = self.view.load_image(fileName)
self.fitToWindowAct.setEnabled(is_loaded)
self.updateActions()
def fitToWindow(self):
if self.fitToWindowAct.isChecked():
self.view.fitToWindow()
else:
self.view.resetZoom()
self.updateActions()
def about(self):
QtWidgets.QMessageBox.about(
self,
"ImageViewer",
"ImageViewer",
)
def createActions(self):
self.openAct = QtWidgets.QAction(
"&Open...", self, shortcut="Ctrl+O", triggered=self.open
)
self.exitAct = QtWidgets.QAction(
"E&xit", self, shortcut="Ctrl+Q", triggered=self.close
)
self.zoomInAct = QtWidgets.QAction(
self.tr("Zoom &In (25%)"),
self,
shortcut="Ctrl++",
enabled=False,
triggered=self.view.zoomIn,
)
self.zoomOutAct = QtWidgets.QAction(
self.tr("Zoom &Out (25%)"),
self,
shortcut="Ctrl+-",
enabled=False,
triggered=self.view.zoomOut,
)
self.normalSizeAct = QtWidgets.QAction(
self.tr("&Normal Size"),
self,
shortcut="Ctrl+S",
enabled=False,
triggered=self.view.resetZoom,
)
self.fitToWindowAct = QtWidgets.QAction(
self.tr("&Fit to Window"),
self,
enabled=False,
checkable=True,
shortcut="Ctrl+F",
triggered=self.fitToWindow,
)
self.aboutAct = QtWidgets.QAction(self.tr("&About"), self, triggered=self.about)
self.aboutQtAct = QtWidgets.QAction(
self.tr("About &Qt"), self, triggered=QtWidgets.QApplication.aboutQt
)
def createMenus(self):
self.fileMenu = QtWidgets.QMenu(self.tr("&File"), self)
self.fileMenu.addAction(self.openAct)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.exitAct)
self.viewMenu = QtWidgets.QMenu(self.tr("&View"), self)
self.viewMenu.addAction(self.zoomInAct)
self.viewMenu.addAction(self.zoomOutAct)
self.viewMenu.addAction(self.normalSizeAct)
self.viewMenu.addSeparator()
self.viewMenu.addAction(self.fitToWindowAct)
self.helpMenu = QtWidgets.QMenu(self.tr("&Help"), self)
self.helpMenu.addAction(self.aboutAct)
self.helpMenu.addAction(self.aboutQtAct)
self.menuBar().addMenu(self.fileMenu)
self.menuBar().addMenu(self.viewMenu)
self.menuBar().addMenu(self.helpMenu)
def updateActions(self):
self.zoomInAct.setEnabled(not self.fitToWindowAct.isChecked())
self.zoomOutAct.setEnabled(not self.fitToWindowAct.isChecked())
self.normalSizeAct.setEnabled(not self.fitToWindowAct.isChecked())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())