QGraphicsViewメモ
2020/11/06 categories:PyQt5| tags:PyQt5|Python|
スムーズなズームインズームアウト
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, *argv, **keywords):
super(GraphicsView, self).__init__(*argv, **keywords)
self._numScheduledScalings = 0
def wheelEvent(self, event):
numDegrees = event.angleDelta().y() / 8
numSteps = numDegrees / 15
self._numScheduledScalings += numSteps
if self._numScheduledScalings * numSteps < 0:
self._numScheduledScalings = numSteps
anim = QtCore.QTimeLine(350, self)
anim.setUpdateInterval(20)
anim.valueChanged.connect(self.scalingTime)
anim.finished.connect(self.animFinished)
anim.start()
def scalingTime(self, x):
factor = 1.0 + float(self._numScheduledScalings) / 300.0
self.scale(factor, factor)
def animFinished(self):
if self._numScheduledScalings > 0:
self._numScheduledScalings -= 1
else:
self._numScheduledScalings += 1
def setScrollHandDragMode(self):
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
def setRubberBandDragMode(self):
self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
マウス左ボタンのドラッグで範囲選択、マウス中ボタンのドラッグで表示範囲の移動
from PyQt5 import QtWidgets, QtCore, QtGui
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, *argv, **keywords):
super(GraphicsView, self).__init__(*argv, **keywords)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.MidButton:
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
event = QtGui.QMouseEvent(
QtCore.QEvent.GraphicsSceneDragMove,
event.pos(),
QtCore.Qt.MouseButton.LeftButton,
QtCore.Qt.MouseButton.LeftButton,
QtCore.Qt.KeyboardModifier.NoModifier
)
elif event.button() == QtCore.Qt.LeftButton:
self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
QtWidgets.QGraphicsView.mousePressEvent(self, event)
def mouseReleaseEvent(self, event):
QtWidgets.QGraphicsView.mouseReleaseEvent(self, event)
self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
クリックした位置の取得
from PyQt5 import QtWidgets
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, *argv, **keywords):
super(GraphicsView, self).__init__(*argv, **keywords)
def mousePressEvent(self, event):
QtWidgets.QGraphicsView.mousePressEvent(self, event)
print( 'view position :', event.pos() )
print( 'scene position :', self.mapToScene(event.pos()) )
view position : PyQt5.QtCore.QPoint(180, 124)
scene position : PyQt5.QtCore.QPointF(53.0, 29.0)