from PyQt5.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem, QGraphicsPixmapItem) from PyQt5.QtCore import (QPointF, QPoint, QLineF, QLine, QRectF, QRect, Qt) from PyQt5.QtGui import (QBrush, QPen, QPixmap, QPicture, QImage) import math class ImageView(QGraphicsView): def __init__(self): super(ImageView, self).__init__() self.scene = QGraphicsScene() self.lena = QPixmap(r"C:\Users\cpz\Pictures\temp\4E373925CBFB476dB6F3DD0A523410B7.jpg") self.item = QGraphicsPixmapItem(self.lena) # 创建像素图元 self.scene.addItem(self.item) self.setScene(self.scene) def wheelEvent(self, event): self.scaleView(math.pow(2.0, -event.angleDelta().y() / 240.0)) def scaleView(self, scaleFactor): factor = self.transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width() if factor < 0.07 or factor > 100: return self.scale(scaleFactor, scaleFactor) if __name__ == "__main__": import sys app = QApplication(sys.argv) view = ImageView() view.show() sys.exit(app.exec_())