【问题标题】:Cannot add item to itemgroups if itemchange() is defined (PyQt)如果定义了 itemchange(),则无法将项目添加到项目组 (PyQt)
【发布时间】:2013-05-17 16:47:34
【问题描述】:

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

这很好用,但前提是我没有在我的自定义子项类中定义 itemChange() 方法。一旦我定义了该方法(即使我只是将函数调用传递给超类),无论我尝试什么,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 中正确调用超类方法,还是 QT/PyQT 魔法中的某个地方出现了问题?

我将 Python 3.3 与 PyQt 4.8 和 QT 5 一起使用。

【问题讨论】:

    标签: python qt pyqt itemgroup


    【解决方案1】:

    我遇到了完全相同的问题。也许这样: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。

    【讨论】:

    • 您好,Henry,非常感谢您的回复和努力。不幸的是,有用的邮件列表帖子来自 2013 年 8 月,那时我的 PyQT 已经完成(使用丑陋的变通方法来完全避免出现问题的情况)。无论如何,我有时会尝试让旧的东西再次运行,以检查您的解决方案是否有帮助...... :-) 再次感谢分享!
    猜你喜欢
    • 2016-10-19
    • 2019-11-23
    • 1970-01-01
    • 2018-08-03
    • 2020-12-23
    • 1970-01-01
    • 2013-04-19
    • 2019-09-27
    相关资源
    最近更新 更多