chezifresh
23rd April 2009, 02:37
I have a QGraphicsView where I'm allowing the user to change the zoom using CTRL-MouseWheel. The way QGraphicsView::scale(float, float) is setup it requires you to store the previous scale.
Ex:
def wheelEvent(self, evt):
mods = evt.modifiers()
if mods == Qt.ControlModifier and evt.orientation() == Qt.Vertical:
# Most mouse types work in steps of 15 degrees
# in which case the delta value is a multiple of 120;
# i.e., 120 units * 1/8 = 15 degrees
numDegrees = evt.delta() / 8
numSteps = numDegrees / 15
newScale = self._scale + ((5*numSteps)/100.0)
if newScale > 0.0 and newScale < 5.0:
self.resetMatrix()
self.scale(newScale, newScale)
self._scale = newScale
return
QtGui.QGraphicsView.wheelEvent(self, evt)
First off... is there a better way of doing this?
This all falls apart because it relies on the fact that "self._scale" represents the current scale factor applied to the matrix. As well as a myriad of other issues that are caused by calling resetMatrix().
So I was wondering if there is a way to derive the current scale factor. I assumed there isn't since its not part of the public API... so I'm hoping someone has come up with a smarter way to zoom in/out.
By the way this works fine if I just want to zoom in/out but I'm also doing a fitToSelection() call which messes up everything.
Full code is available here in case anyone is interested:
http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-browse-images-in-thumbnail-view-15739.html
Ex:
def wheelEvent(self, evt):
mods = evt.modifiers()
if mods == Qt.ControlModifier and evt.orientation() == Qt.Vertical:
# Most mouse types work in steps of 15 degrees
# in which case the delta value is a multiple of 120;
# i.e., 120 units * 1/8 = 15 degrees
numDegrees = evt.delta() / 8
numSteps = numDegrees / 15
newScale = self._scale + ((5*numSteps)/100.0)
if newScale > 0.0 and newScale < 5.0:
self.resetMatrix()
self.scale(newScale, newScale)
self._scale = newScale
return
QtGui.QGraphicsView.wheelEvent(self, evt)
First off... is there a better way of doing this?
This all falls apart because it relies on the fact that "self._scale" represents the current scale factor applied to the matrix. As well as a myriad of other issues that are caused by calling resetMatrix().
So I was wondering if there is a way to derive the current scale factor. I assumed there isn't since its not part of the public API... so I'm hoping someone has come up with a smarter way to zoom in/out.
By the way this works fine if I just want to zoom in/out but I'm also doing a fitToSelection() call which messes up everything.
Full code is available here in case anyone is interested:
http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-browse-images-in-thumbnail-view-15739.html