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

我正在构建一个PyQt QGraphicsView项目,其中一些QGraphicItem可以在不同的QGraphicsItemGroup之间移动.为此,我使用“new”父itemGroup的addItemToGroup()方法.

这工作正常,但只要我没有在自定义子项类中定义itemChange()方法.一旦我定义了该方法(即使我只是将函数调用传递给超类),无论我尝试什么,都不会将childItem添加到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

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

我使用Python 3.3与PyQt 4.8和QT 5.

最佳答案 我有同样的问题.也许这个:
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

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

也许不是最优雅和通用的解决方案,但在这里,它可以工作 – 我能够再次将QGraphicItems添加到QGraphicItemGroups.

点赞