Unsure how QGraphicsView and QGraphicsScene work after scaling

31 Views Asked by At

My goal is to have a QGraphicsView widget in which there will be a photo of unknown size. I should be able to move around by dragging the image, and zoom in and out.

I have set QGraphicsScene(0, 0, 300, 300) and the QGraphicsView will be of fixed size of 300, 300. I expect image to be larger than that, that is why I will need an option to move around.

For this I have implemented a custom QGraphicsView class in which I am going to override some mouseEvent methods:

class MyGraphicsView(QGraphicsView):
    def __init__(self, parent: QWidget) -> None:
        super().__init__(parent=parent)
        self._scene = QGraphicsScene()
        self.setFixedSize(600, 600)
        self.setScene(QGraphicsScene(0, 0, 600, 600))

    def mousePressEvent(self, event):
        self._movement_enabled = True
        self._last_pos = event.pos()

    def mouseReleaseEvent(self, event):
        self._movement_enabled = False

    def mouseMoveEvent(self, event):
        if self._movement_enabled:
            pos = event.pos()
            delta = pos - self._last_pos

            rect = self.sceneRect().translated(-delta.x(), -delta.y())
            self.setSceneRect(rect)
            logger.debug(f"New Rect: {rect}")

            self._last_pos = pos

Moving around works fine until I introduce zooming with a wheelEvent

    def wheelEvent(self, event):
        factor = 0.8 if event.angleDelta().y() < 0 else 1.2
        self.scale(factor, factor)

Zooming in/out on its own works as expected. However, the problem arises, when I have a different scale and I try to move around.

At the start, I have a default scale. The image uploaded is 1280x720. I have moved the left corner of the image to the left corner of MyGraphicsView and everything seems reasonable, I have .rectScene() of QRectF(0, 0, 300, 300) which is what I expect: I am trying to be in the left corner of the image and that is what I get.

I Scrolled few times to zoom out and I have moved my image to the left corner of the QGraphicsView, and now my .rectScene() is QRectF(163, 163, 300, 300).

After that, I have zoomed in. Now, when I try to move around when I am zoomed in, it appears that I have dead zone, where moving around does not work at the start of the dragging and only image starts to move around when I have dragged mouse a little bit more.

I now appear to be on the edge horizontally and almost 300 pixels outside of the scene, which seems reasonable:

Now I started dragging up so that the image comes into the scene, and there was a dead zone, I needed to drag for ~130 pixels for scene to start moving, until then, me dragging would do nothing, there was a dead zone, and the scene was not moving, even though I was dragging.

This is not an issue, however, when I am zoomed out and moving around works fine when zoomed out. However, I still find it odd that sceneRect() is QRectF(567, 567, 300, 300) even though I am in a top left corner.

All of this made me confused. I am sure it has to do something with scaling of QGraphicsView and I am not sure how that works, I am trying to read docs, but all of it is not exactly very clear for me, how does all of the transforming relate to scaling and I could use help how to solve my issue of moving around not working properly when zoomed in

0

There are 0 best solutions below