mouseMoveEvent 在 QGraphicsScene 中不起作用

发布于 2024-09-30 17:15:56 字数 456 浏览 1 评论 0 原文

我正在尝试在 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 中完美运行。我有什么遗漏的吗?

I am trying to implement a paint program in PyQt4. In the QGraphicsScene, I use the mouseMoveEvent to draw lines connecting the consecutive mouse co-ordinates.
The code is as follows:

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 are initialized as 0,0.

The problem I encounter is that, when I click the mousebutton a single point is drawn at (0,0), and no more drawing takes place.
The same mouseMoveEvent works perfectly in QGraphicsView. Is there anything that I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-10-07 17:15:56

我发现了问题所在。上面的代码很好,只是 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 use event.scenePos()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文