Cannot add item to itemgroups if itemchange () is defined (PyQt)

I am creating a PyQt QGraphicsView project where some QGraphicItems can move between different QGraphicsItemGroups. To do this, I use the addItemToGroup () method for the "new" parent group of elements.

This works fine, but only until I define the itemChange () method in my child class. As soon as I define this method (even if I just pass the function call to the superclass), 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 just too stupid to properly call the superclass method in Python, or is this a problem somewhere in QT / PyQT magic?

I am using Python 3.3 with PyQt 4.8 and QT 5.

+4
source share
1 answer

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

Update: In fact, I 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 here: http://www.mail-archive.com/ pyqt@riverbankcomputing.com /msg26190.html

This may not be the most elegant and general solution, but it works here - I can add QGraphicItems to QGraphicItemGroups again.

+2
source

Source: https://habr.com/ru/post/1481434/


All Articles