mouseMoveEvent 在 QGraphicsScene 中不起作用
我正在尝试在 PyQt4 中实现一个绘图程序。在 QGraphicsScene 中,我使用 mouseMoveEvent 绘制连接连续鼠标坐标的线。
代码如下:
def mouseMoveEvent(self, event):
x1 = event.pos().x()
y1 = event.pos().y()
self.addLine(self.x0, self.y0, x1, y1, self.pen)
self.x0, self.y0 = x1, y1
x0,y0初始化为0,0。
我遇到的问题是,当我单击鼠标按钮时,在 (0,0) 处绘制一个点,并且不再进行绘制。
相同的 mouseMoveEvent 在 QGraphicsView 中完美运行。我有什么遗漏的吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题所在。上面的代码很好,只是
event.pos()
在 QGraphicsScene 中不起作用。为了捕获QGraphicsScene中mouseEvent的坐标,我们必须使用event.scenePos()
I figured out the problem. The above code is fine, except that
event.pos()
does not work in QGraphicsScene. Inorder to capture the mouseEvent's co-ordinates in QGraphicsScene, we must useevent.scenePos()