如何将 QGraphicsView 缩放恢复到 100%,即程序启动时的缩放?
How to restore a QGraphicsView zoom to 100%, i.e., the zoom when the program started?
到目前为止,我有这个代码:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
"""
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?
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_()
它生成这个 window 最初打开为:
但是后者我可以像这样放大:
然后我想要一个重置缩放按钮,在程序打开时将缩放设置为默认缩放:
但是我现在的按钮只能全屏显示可见部分,鼠标滚动缩放后不会恢复原来的缩放:
要重置索引,您必须重置转换:
{your_graphicsview}.setTransform(QtGui.QTransform())
到目前为止,我有这个代码:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
"""
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?
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_()
它生成这个 window 最初打开为:
但是后者我可以像这样放大:
然后我想要一个重置缩放按钮,在程序打开时将缩放设置为默认缩放:
但是我现在的按钮只能全屏显示可见部分,鼠标滚动缩放后不会恢复原来的缩放:
要重置索引,您必须重置转换:
{your_graphicsview}.setTransform(QtGui.QTransform())