qgraphicsview wheel to zoom
时间: 2023-09-09 14:10:12 浏览: 113
QGraphicsView has built-in support for zooming using the mouse wheel. Here's how you can use it:
1. Enable mouse tracking on the view:
```python
view.setMouseTracking(True)
```
2. Connect the `wheelEvent` of the view to a slot that handles the zooming:
```python
view.wheelEvent = self.handleWheelEvent
```
3. In the slot, adjust the view's zoom level based on the mouse wheel delta:
```python
def handleWheelEvent(self, event):
delta = event.angleDelta().y()
zoomFactor = 1.2
if delta > 0:
self.view.scale(zoomFactor, zoomFactor)
else:
self.view.scale(1 / zoomFactor, 1 / zoomFactor)
```
This code will zoom the view in/out by a factor of 1.2 each time the mouse wheel is scrolled up/down. You can adjust the zoom factor to suit your needs.
阅读全文