我正在尝试从 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_())
非常欢迎提示和提示。
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_())
问题是
text
已被删除,因为从组中删除它后您没有任何引用它,请尝试以下操作:
...
text = QGraphicsTextItem()
scene.text = text #just to keep the reference, ideally should be self.text = text
...
现在你不需要
scene.addItem(text)