5

I'm building a PyQt QGraphicsView project where some QGraphicItems can be moved around between different QGraphicsItemGroups. For that, I use the addItemToGroup() method of the "new" parent itemGroup.

This works fine, but only so long as I do not define the itemChange() method in my custom child-item class. Once I define that method (even if I just pass the function call to the super class), the childItems will not be added to ItemGroups no matter what I try.

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

Am I simply too stupid for properly calling a superclass method in Python, or is the problem somewhere in the QT / PyQT magic?

I use Python 3.3 with PyQt 4.8 and QT 5.

1 Answers1

2

I had the exact same problem. Maybe this: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg27457.html answers some of your questions? Seems like we might be out of luck in PyQt4.

Update: Actually, just found a workaround:

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

taken from here: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg26190.html

Maybe not the most elegant and general solution, but over here, it works -- I'm able to add QGraphicItems to QGraphicItemGroups again.

Henry
  • 141
  • 9
  • Hi Henry, thank you very much for your response and your effort. Unfortunately, the helpful mailing list posts were from August 2013, by they my PyQT was already finished (using ugly workarounds to avoid the problematic situation altogether). Anyway, I will try to get the old stuff running sometimes again just in order to check whether your solution might have helped... :-) Thank again for sharing! – Christian Zöllner Dec 16 '13 at 23:30