【问题标题】:PyQt4: QGraphicsItem mousePressEvent() disables flag ItemIsMovablePyQt4:QGraphicsItem mousePressEvent() 禁用标志 ItemIsMovable
【发布时间】:2016-04-25 08:42:47
【问题描述】:

这是一个错误,我不小心解决了它,但不知道为什么会这样。我希望有人可以向我解释其背后的逻辑。

我重新实现了 QGraphicsItem 及其 mousePressEvent。 通过这样做,该项目不再是可移动的。 即使尝试拨打QGraphicsItem.mousePressEvent(self, event) 也不起作用。 只有当我重新实现 mouseMoveEvent() 和 mouseReleaseEvent() 时,它才最终起作用。

代码:

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)

【问题讨论】:

  • Qt C++ documentation 解释了 Qt 如何决定向哪个项目提供鼠标事件。也许它有帮助(我不能 100% 确定它是否能解释您所看到的,我认为这取决于您对 mousePressEvent() 的实施)

标签: mouseevent pyqt4 qgraphicsitem mousepress


【解决方案1】:

鼠标事件沿父部件链向上传播,直到出现一个部件 使用 accept() 接受它,或者事件过滤器使用它。

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

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

添加其他两个函数(mouseMoveEventmouseReleaseEvent)必须与您添加的同时发生

QtGui.QGraphicsItem.mousePressEvent(self, event)

转至您的mousePressEvent - 这就是它似乎解决了问题的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-06
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2023-04-06
    • 2014-03-11
    相关资源
    最近更新 更多