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