2

I have created a QGraphicsView and inside it a QGraphicsScene. I load a pixmap in the QGraphicsScene. Actually it's an inherited QGraphicsScene which implements a wheelEvent in order to make the zoom in/out function. Zoom function scales the pixmap by 10% up or down depending on the wheel's rotation. The scaling works fine, however the QGraphicsView size or QGraphicsScene size gets the value of maximum size of the rotation that was tried and not center any more. For example, If I scale it up using the wso that the scroll bars enabled then if I scale it down the scroll bars still are enabled and the pixmap goes at the top-left corner. Any suggestions?

Edit: The scale function of QGraphicsView creates a low quality pixmap, that's why I don't use it. I'm adding a scaled pixmap and at every rotation I scale it again and project to GraphicsView. However the the size of GraphicsView or QGraphicsScene has resizing problems as mentioned before.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
intergallactic
  • 138
  • 1
  • 11
  • 1
    see if my question helps http://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel – AngryDuck Apr 23 '14 at 14:35
  • @AngryDuck the problem with the scale function of the QGraphicsView, is that it creates a low quality view of the pixmap, and that's the reason I don't use it. – intergallactic Apr 23 '14 at 17:37
  • hang on if you just want to load a different resolution image with each wheel scroll instead of actually scaling the image as it makes its "pixelated" then just have the mouse wheel event clear the scene and just add either larger or smaller image to the graphicsview depending on mouse scroll (not ideal but seems like all your asking for) – AngryDuck Apr 24 '14 at 08:22
  • @AngryDuck, this is what I want. The problem was that the scene didn't follow the size of the pixmap. For example, when I was scrolling and created a large pixmap so that scroll bars were needed then if I was scrolling and created a small pixmap the scroll bars were still there cause the size of scene didn't change. However I found the solution to that by setting a rect in scene after adding the new pixmap. – intergallactic Apr 24 '14 at 10:22

2 Answers2

2

After a long time I found the solution to that. The problem was that the scene didn't follow the size of the pixmap. For example, when I was scrolling and created a large pixmap so that scroll bars were needed then if I was scrolling and created a small pixmap the scroll bars were still there cause the size of scene didn't change. However I found the solution to that by setting a rect in scene after adding the new pixmap. So Inside the wheelEvent (self, event) function, after clearing the scene by self.clear() and adding the new scaled pixmap, the size of scene need to change too. To do that I used the below command where self.__size is the new size of the scaled pixmap.

self.setSceneRect(QtCore.QRectF(0.0, 0.0, self.__size.width(), self.__size.height()))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
intergallactic
  • 138
  • 1
  • 11
0

Don't subclass the QGraphicsScene. Subclass the QGraphicsView to get the wheel events, and change the view transformation accordingly. Set the world transform on the graphicsview using setTransform.

This is the proper way of doing it. Conceptually, the objects on the scene do not change size. It is your view that changes, you are zooming in/out. That's the camera moving, not the objects growing/shrinking.

You can use the level of detail in the paint method of the item to paint high or low-res versions of the image depending on the zoom factor.

There is a nice demo with 4000 chips where level of detail is used.

Bgie
  • 523
  • 3
  • 10