问题
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.
回答1:
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()))
回答2:
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.
来源:https://stackoverflow.com/questions/23246246/qgraphicsview-doesnt-resize-after-pixmap-scale-while-pixmap-not-centered-any-m