Python QGraphicsSiteMgroup.removeFromGroup--未将子项正确重新租入场景

Python QGraphicsSiteMgroup.removeFromGroup--未将子项正确重新租入场景,python,qt,pyqt,Python,Qt,Pyqt,我正在尝试从QGraphicsSiteMgroup中删除QGraphicsItem。调用removeFromGroup时,该项被删除(当然)。但是,它在场景中不再可见。我必须调用Scene.addItem(项目),以便它再次出现。这显然是你不应该做的事情(我因此受到警告)。但我似乎找不到其他解决办法 下面是一个简单的例子: import sys from PyQt4.QtGui import * from PyQt4.QtCore import * class MainWindow(QMa

我正在尝试从QGraphicsSiteMgroup中删除QGraphicsItem。调用removeFromGroup时,该项被删除(当然)。但是,它在场景中不再可见。我必须调用Scene.addItem(项目),以便它再次出现。这显然是你不应该做的事情(我因此受到警告)。但我似乎找不到其他解决办法

下面是一个简单的例子:

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_())

提示和提示非常受欢迎。

问题是
文本
被删除,因为您从组中删除后没有任何引用,请尝试以下操作:

...
text = QGraphicsTextItem()
scene.text = text #just to keep the reference, ideally should be self.text = text
...

现在您不需要
场景。添加项(文本)

QGraphicsTextItem永远不能作为场景的父项,因为它的父项必须是QGraphicsSitem(QGraphicsCene不会继承)

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

调用
scene.addItem()
实际上是禁止的。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_())

在将文本项添加到组之前,请尝试将其添加到场景中谢谢您的建议,但(至少在这里)没有任何区别。请在从组中删除后尝试设置该项的位置。