gpt4 book ai didi

c++ - 如何删除具有一些子项作为成员的 QGraphicsItem?

转载 作者:太空狗 更新时间:2023-10-29 20:28:02 25 4
gpt4 key购买 nike

我有一个继承 QGraphicsItem 的自定义项目。它有一些子图形项作为成员变量。我应该按照什么顺序从场景中删除这些项目以及如何删除它们?

最佳答案

像这样创建您的成员(member)图形项目:

class MyClass : public QGraphicsItem
{
...
private:

SomeKindOfGraphicsItem* _item1;
SomeOtherGraphicsItem* _item2;
}

MyClass::MyClass( ... ) :
QGraphicsItem( ... ),
_item1( new SomeKindOfGraphicsItem( ..., this ) ),
_item2( new SomeOtherGraphicsItem( ..., this ) )
{
...
}

在这种情况下,只需从场景中移除父项目(本例中的 MyClass)就足够了。这也将删除所有子项:

void QGraphicsScene::removeItem(QGraphicsItem * item)

Removes the item item and all its children from the scene. The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed).

此外,当 MyClass 的对象被删除时,它将通过 Qt 机制删除其所有子对象:

QGraphicsItem::~QGraphicsItem() [virtual]

Destroys the QGraphicsItem and all its children. If this item is currently associated with a scene, the item will be removed from the scene before it is deleted.

Note: It is more efficient to remove the item from the QGraphicsScene before destroying the item.

关于c++ - 如何删除具有一些子项作为成员的 QGraphicsItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14438202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com