So, far I have this code:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
"""
Python PyQt: How can I move my widgets on the window with mouse?
https://stackoverflow.com/questions/12213391/python-pyqt-how-can-i-move-my-widgets-on-the-window-with-mouse
"""
def __init__( self, parent = None ):
super( MyFrame, self ).__init__( parent )
scene = QtGui.QGraphicsScene()
self.setScene( scene )
self.resize( 400, 340 )
# http://pyqt.sourceforge.net/Docs/PyQt4/qpen.html
pencil = QtGui.QPen( QtCore.Qt.black, 1)
pencil.setStyle( QtCore.Qt.SolidLine )
scene.addLine( QtCore.QLineF( 0, -50, 0, 50 ), pencil )
scene.addLine( QtCore.QLineF( -50, 0, 50, 0 ), pencil )
fitInViewButton = QtGui.QPushButton( 'Fit In View', self )
fitInViewButton.clicked.connect( self.handleFitInView )
scene.addWidget( fitInViewButton )
def handleFitInView( self ):
# Auto scale a QGraphicsView
# http://www.qtcentre.org/threads/42917-Auto-scale-a-QGraphicsView
self.ensureVisible ( self.scene().itemsBoundingRect() )
self.fitInView( self.scene().itemsBoundingRect(), QtCore.Qt.KeepAspectRatio )
def wheelEvent( self, event ):
"""
PyQT4 WheelEvent? how to detect if the wheel have been use?
https://stackoverflow.com/questions/9475772/pyqt4-wheelevent-how-to-detect-if-the-wheel-have-been-use
QGraphicsView Zooming in and out under mouse position using mouse wheel
https://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
"""
# Zoom Factor
zoomInFactor = 1.1
zoomOutFactor = 1 / zoomInFactor
# Set Anchors
self.setTransformationAnchor( QtGui.QGraphicsView.NoAnchor )
self.setResizeAnchor( QtGui.QGraphicsView.NoAnchor )
# Save the scene pos
oldPos = self.mapToScene( event.pos() )
# Zoom
if event.delta() > 0:
zoomFactor = zoomInFactor
else:
zoomFactor = zoomOutFactor
self.scale( zoomFactor, zoomFactor )
# Get the new position
newPos = self.mapToScene( event.pos() )
# Move scene to old position
delta = newPos - oldPos
self.translate( delta.x(), delta.y() )
if ( __name__ == '__main__' ):
app = QtGui.QApplication( [] )
f = MyFrame()
f.show()
app.exec_()
It generates this window which initially opens as:
However latter I can zoom in like:
Then I would like to have a reset zoom button which sets the zoom as the default zoom when the program opened:
But the button I have for now is only fitting the visible parts in the full screen, instead of restore the original zoom, after the zoom scroll by mouse:
To reset the index you must reset the transformation:
{your_graphicsview}.setTransform(QtGui.QTransform())