This took me forever to figure out. I noticed lots of people online had the same question but few got it working properly and some had to do a hack. Here is the clean way to do this. The trick is in setTransformationAnchor() and setDragMode()
My setup code in my QDailogs __init__ function:
imageFilename = QFileDialog.getOpenFileName(self,'select image') self._pixMap = QPixmap.fromImage(QImage(imageFilename)) self._graphicsScene = QGraphicsScene(self) self._graphicsScene.addPixmap(self._pixMap) self._view = MyGraphicsView() self._view.setScene(self._graphicsScene) # Create a vertical layout. self._layout = QVBoxLayout() self._layout.setMargin(0) self._layout.setSpacing(0) # Add label widget self._layout.addWidget(self._view) # set widget to layout self.setLayout(self._layout)
Here is my implementation of the MyGraphicsView class. Derived from QGraphicsView
class MyGraphicsView(QGraphicsView): def __init__(self): QGraphicsView.__init__(self) self.setRenderHints(QPainter.Antialiasing|QPainter.SmoothPixmapTransform) self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse) self.setDragMode(QGraphicsView.ScrollHandDrag) def wheelEvent(self,event): adj = (event.delta()/120) * 0.1 self.scale(1+adj,1+adj)