我想删除Qt3DWindow根场景的所有根节点。它包含节点的多个层次结构级别。我想删除引用并删除对象。最简单的方法是什么?

最佳答案

我使用此递归函数来做到这一点:

void deleteChildrenRecursively(Qt3DCore::QNodeVector& vector)
{
    foreach(Qt3DCore::QNode* node, vector){
        Qt3DCore::QEntity* entity = (Qt3DCore::QEntity*)node;
        QList<Qt3DCore::QComponent*> componentsToDelete;
        foreach(Qt3DCore::QComponent* component, entity->components()){
            entity->removeComponent(component);
            delete(component);
            component = NULL;
        }
        deleteChildrenRecursively(node->childNodes());
        delete(node);
        node = NULL;
    }
}

它递归删除所有QEntity及其QComponent对象。

用法:
Qt3DCore::QEntity* rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity)
...
deleteChildrenRecursively(rootEntity->childNodes());

关于c++ - 如何在Qt3DWindow的根实体中递归删除所有节点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45759274/

10-09 13:15