gpt4 book ai didi

python - 如何将 QGraphicsView 缩放恢复到 100%,即程序启动时的缩放?

转载 作者:太空宇宙 更新时间:2023-11-04 02:49:43 26 4
gpt4 key购买 nike

到目前为止,我有这段代码:

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_()

它生成这个最初打开的窗口:

enter image description here

但是后者我可以像这样放大:

enter image description here

然后我想要一个重置缩放按钮,在程序打开时将缩放设置为默认缩放:

enter image description here

但是我现在的按钮只适合全屏的可见部分,而不是恢复原始缩放,在鼠标滚动缩放后:

enter image description here

最佳答案

要重置索引,您必须重置转换:

{your_graphicsview}.setTransform(QtGui.QTransform())

关于python - 如何将 QGraphicsView 缩放恢复到 100%,即程序启动时的缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44270301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com