如果定义了 itemchange() 则无法将项目添加到项目组 (PyQt)

Chr*_*ner 5 python qt pyqt itemgroup

我正在构建一个 PyQt QGraphicsView 项目,其中一些 QGraphicItems 可以在不同的 QGraphicsItemGroups 之间移动。为此,我使用“新”父项组的 addItemToGroup() 方法。

这工作正常,但前提是我没有在自定义子项类中定义 itemChange() 方法。一旦我定义了该方法(即使我只是将函数调用传递给超类),无论我如何尝试,子项都不会添加到 ItemGroups 中。

class MyChildItem(QtGui.QGraphicsItemGroup):
    def itemChange(self, change, value):
        # TODO: Do something for certain cases of ItemPositionChange
        return QtGui.QGraphicsItemGroup.itemChange(self, change, value)
        #return super().itemChange(change, value)   # Tried this variation too
        #return value   # Tried this too, should work according to QT doc
Run Code Online (Sandbox Code Playgroud)

我是否太愚蠢而无法在Python中正确调用超类方法,或者问题出在QT / PyQT魔法中的某个地方?

我使用 Python 3.3 以及 PyQt 4.8 和 QT 5。

Hen*_*nry 2

我有同样的问题。也许这个: http: //www.mail-archive.com/pyqt@riverbankcomputing.com/msg27457.html可以回答您的一些问题?看来我们在 PyQt4 中可能不走运。

更新:实际上,刚刚找到了解决方法:

import sip

def itemChange(self, change, value):
        # do stuff here...
        result = super(TestItem, self).itemChange(change, value)
        if isinstance(result, QtGui.QGraphicsItem):
            result = sip.cast(result, QtGui.QGraphicsItem)
        return result
Run Code Online (Sandbox Code Playgroud)

摘自这里:http ://www.mail-archive.com/pyqt@riverbankcomputing.com/msg26190.html

也许不是最优雅和最通用的解决方案,但在这里,它有效——我能够再次将 QGraphicItems 添加到 QGraphicItemGroups 中。