QGraphicsItemGroup.removeFromGroup -- 子项未正确重新定位到场景

Hen*_*nry 3 python qt pyqt qgraphicsview

我正在尝试从 QGraphicsItemGroup 中删除 QGraphicsItem。当调用removeFromGroup时,该项目被删除(当然)。但是,它在场景中不再可见。我必须调用 Scene.addItem(item) 才能使其再次出现。这显然是你不应该做的事情(我因为这样做而收到了警告)。但我似乎找不到其他解决方法。

这是一个最小的例子:

import sys 

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.view = QGraphicsView()
        self.scene = QGraphicsScene()
        self.view.setScene(self.scene)

        self.setCentralWidget(self.view)


def add_group(scene):
    group = QGraphicsItemGroup()
    text = QGraphicsTextItem()
    text.setPlainText("I'm visible")
    group.addToGroup(text)
    scene.addItem(group)

    # After this, text is no longer in group. However, it is no longer visible.
    group.removeFromGroup(text)
    assert not text in group.childItems()

    # But text is still in scene. 
    assert text.scene() == scene

    # this works (i.e. text becomes visible again). However, it also produces a 
    # warning: QGraphicsScene::addItem: item has already been added to this scene. 
    # The docs also advice against it.
    scene.addItem(text)

    # According to the docs, I thought this might work, but it gives me a TypeError.
    # text.setParentItem(0)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    add_group(main.scene)
    main.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

非常欢迎提示和提示。

ekh*_*oro 5

QGraphicsTextItem 永远不能成为场景的父级,因为它的父级必须是 QGraphicsItem(QGraphicsScene 不继承)。

当 QGraphicsTextItem 创建时,它的父项是None。它的父级在添加到它时设置为group(QGraphicsItemGroup 是 QGraphicsItem 的子类),然后设置回None当它从 中删除时group

调用scene.addItem()实际上是一个NO-OP。Qt 检查 是否scene与 相同text.scene(),如果相同,则打印警告并返回而不执行任何操作。

事实上,它在某些情况下似乎“有效”,只是 python 垃圾收集机制的一个产物。

如果您的测试以更真实的方式重新铸造,则 QGraphicsTextItem 从组中删除后仍然可见:

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.view = QGraphicsView(self)
        self.scene = QGraphicsScene(self.view)
        self.view.setScene(self.scene)
        self.setCentralWidget(self.view)
        self.group = QGraphicsItemGroup()
        self.text = QGraphicsTextItem()
        self.text.setPlainText("I'm visible")
        self.group.addToGroup(self.text)
        self.scene.addItem(self.group)
        self.group.removeFromGroup(self.text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)