本文介绍了PyQt4:QGraphicsItem mousePressEvent()禁用标志ItemIsMovable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个错误,我无意中解决了它,却不知道为什么会起作用.我希望有人可以向我解释其背后的逻辑.

Here's a bug which I accidently solved it and have no idea why that works. I hope that someone could explain to me the logics behind it.

我重新实例化了QGraphicsItem及其mousePressEvent.这样一来,该物品就不再可移动了.即使在尝试调用QGraphicsItem.mousePressEvent(self, event)时,它也不起作用.只有当我重新配置mouseMoveEvent()和mouseReleaseEvent()时,它才有效.

I've reimplmented QGraphicsItem and its mousePressEvent.By doing that the item was no longer movable.Even when trying to call QGraphicsItem.mousePressEvent(self, event) it didn't work.Only when I reimplmented mouseMoveEvent() and mouseReleaseEvent() it finally worked.

代码:

class LWResizeableItem(QtGui.QGraphicsItem):

    def __init__(self):

        super(LWResizeableItem, self).__init__()
        self.setFlag(QtGui.QGraphicsItem.ItemIsMovable)

    def mousePressEvent(self, event):
        QtGui.QGraphicsItem.mousePressEvent(self, event)
        < some code.... >

    def mouseMoveEvent(self, event):
        QtGui.QGraphicsItem.mouseMoveEvent(self, event)

    def mouseReleaseEvent(self, event):
        QtGui.QGraphicsItem.mouseReleaseEvent(self, event)

推荐答案

我的猜测是,由于您未显示相关代码,因此您的mousePressEvent接受了该事件.这阻止了QtGui的处理(您的代码完成了所有处理).

My guess, since you did not show relevant code, is that your mousePressEvent accepted the event. That prevented QtGui from handling it (Your code did all the handling).

您通过调用执行默认功能(除了您自己的功能)的QtGui.QGraphicsItem.mousePressEvent解决了错误".

You solved the "bug" by calling QtGui.QGraphicsItem.mousePressEvent which performs the default function (in addition to your own).

添加其他两个功能(mouseMoveEventmouseReleaseEvent)必须与添加

Adding the other two functions (mouseMoveEvent and mouseReleaseEvent) must have coincided with your adding the

QtGui.QGraphicsItem.mousePressEvent(self, event)

一行到您的mousePressEvent-这就是为什么它似乎已经解决了问题的原因.

line to your mousePressEvent - that is why it seemed to have solved the problem.

这篇关于PyQt4:QGraphicsItem mousePressEvent()禁用标志ItemIsMovable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 16:34