所以,我有这个代码:
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_()
它会生成此窗口,该窗口最初打开为:
然而,后者我可以放大:
然后我想有一个重置缩放按钮,在程序打开时将缩放设置为默认缩放:
但是我现在使用的按钮只是在整个屏幕上显示可见部分,而不是在鼠标缩放滚动后恢复原始缩放:
答案 0 :(得分:2)
要重置索引,您必须重置转换:
{your_graphicsview}.setTransform(QtGui.QTransform())