我正在构建一个 PyQt QGraphicsView 项目,其中一些 QGraphicItem 可以在不同的 QGraphicsItemGroup 之间移动。为此,我使用"new"父项组的 addItemToGroup() 方法。
这工作正常,但前提是我没有在我的自定义子项类中定义 itemChange() 方法。一旦我定义了该方法(即使我只是将函数调用传递给父类(super class)),无论我尝试什么,childItems 都不会添加到 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 中正确调用父类(super class)方法,还是 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。
关于python - 如果定义了 itemchange(),则无法将项目添加到项目组 (PyQt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16614187/