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

标签 python qt pyqt itemgroup

我正在构建一个 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/

相关文章:

python - 自定义模块的代码完成不适用于 PyDev

python - 使用 Privoxy Proxy for Tor 时,Scrapy 出现 NoneType 错误

python - 如何使用 opencv、python、numpy 和必要的库修复下面损坏的图像

mysql - Qt MySQL 驱动程序不支持事务

java - 如何使用 Java Android SDK 做好实时数据流

installation - 如何为 Windows 7 安装 PyQT 的步骤?

python - 如何将字符串解析为 float 或整数?

c++ - 从 QT 中的缓冲区加载像素图

python - 用于安全密码处理的 pyside 小部件

python - 从 QAction 触发器访问委托(delegate)类