diff --git a/src/skeletondocument.cpp b/src/skeletondocument.cpp index 74f62669..d483e5ea 100644 --- a/src/skeletondocument.cpp +++ b/src/skeletondocument.cpp @@ -1534,14 +1534,15 @@ void SkeletonDocument::removeComponentRecursively(QUuid componentId) return; } - for (const auto &childId: component->second.childrenIds) { - removeComponent(childId); - } - if (!component->second.linkToPartId.isNull()) { removePartDontCareComponent(component->second.linkToPartId); } + auto childrenIds = component->second.childrenIds; + for (const auto &childId: childrenIds) { + removeComponentRecursively(childId); + } + QUuid parentId = component->second.parentId; if (!parentId.isNull()) { auto parentComponent = componentMap.find(parentId); diff --git a/src/skeletonparttreewidget.cpp b/src/skeletonparttreewidget.cpp index 51f6aac0..2ed293bb 100644 --- a/src/skeletonparttreewidget.cpp +++ b/src/skeletonparttreewidget.cpp @@ -375,6 +375,24 @@ void SkeletonPartTreeWidget::addComponentChildrenToItem(QUuid componentId, QTree } } +void SkeletonPartTreeWidget::deleteItemChildren(QTreeWidgetItem *item) +{ + auto children = item->takeChildren(); + while (!children.isEmpty()) { + auto first = children.takeFirst(); + auto componentId = QUuid(first->data(0, Qt::UserRole).toString()); + const SkeletonComponent *component = m_document->findComponent(componentId); + if (nullptr != component) { + m_componentItemMap.erase(componentId); + if (!component->linkToPartId.isNull()) { + m_partItemMap.erase(component->linkToPartId); + } + } + deleteItemChildren(first); + delete first; + } +} + void SkeletonPartTreeWidget::componentChildrenChanged(QUuid componentId) { QTreeWidgetItem *parentItem = findComponentItem(componentId); @@ -382,7 +400,7 @@ void SkeletonPartTreeWidget::componentChildrenChanged(QUuid componentId) qDebug() << "Find component item failed:" << componentId; return; } - qDeleteAll(parentItem->takeChildren()); + deleteItemChildren(parentItem); addComponentChildrenToItem(componentId, parentItem); // Fix the last item show in the wrong place sometimes diff --git a/src/skeletonparttreewidget.h b/src/skeletonparttreewidget.h index 5bb22438..5dd1866a 100644 --- a/src/skeletonparttreewidget.h +++ b/src/skeletonparttreewidget.h @@ -67,6 +67,7 @@ protected: virtual void mousePressEvent(QMouseEvent *event); private: void addComponentChildrenToItem(QUuid componentId, QTreeWidgetItem *parentItem); + void deleteItemChildren(QTreeWidgetItem *item); private: const SkeletonDocument *m_document = nullptr; std::map m_partItemMap;