gpt4 book ai didi

python - 如何通过鼠标中键在QGraphicsView 中滚动QGraphicsScene?

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:46 26 4
gpt4 key购买 nike

我用的是pyqt4.8和python2.7

我创建 QGraphicsView 并插入 QGraphicsScene。场景比 View 大,需要滚动。 View 有垂直和水平滚动,但我想通过鼠标中键滚动。

我创建了鼠标事件函数,但移动很奇怪。

def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == Qt.MidButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
else:
super(MyView, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if event.buttons() == Qt.MidButton:
curPos = self.__mousePressPos - self.__mouseMovePos
self.centerOn(curPos.x(), curPos.y())
self.__mouseMovePos = globalPos
else:
super(MyView, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > 3:
event.ignore()
return
super(MyView, self).mouseReleaseEvent(event)

首先场景移动不对应鼠标移动。我怀疑这归咎于视口(viewport)和场景的相对大小。以及舞台的规模。

其次,我无法将场景移到视口(viewport)之外,它会在到达边缘时停止。

让秒还是可以忍的。让它保持原样,但是如何计算在鼠标移动时移动的场景的正确偏移量,而不管它被捕获到哪里?

最佳答案

我的解决方案不是调用 centerOn 方法,而是使用 QGraphicView 的滚动条:

class MyView(QtGui.QGraphicsView):
def mousePressEvent(self, event):
if event.button() == Qt.MidButton: # or Qt.MiddleButton
self.__prevMousePos = event.pos()
else:
super(MyView, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if event.buttons() == Qt.MidButton: # or Qt.MiddleButton
offset = self.__prevMousePos - event.pos()
self.__prevMousePos = event.pos()

self.verticalScrollBar().setValue(self.verticalScrollBar().value() + offset.y())
self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() + offset.x())
else:
super(MyView, self).mouseMoveEvent(event)

关于python - 如何通过鼠标中键在QGraphicsView 中滚动QGraphicsScene?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702141/

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