Do I need to setParent to nullptr before deleting QWidget that's owned by QStackedWidget

184 Views Asked by At

I have a QStackedWidget which has a bunch of static "pages" but in a couple of cases one page needs to be recreated when it's switched to. Currently I have something like this:

void InsetNavigator::Navigate(InsetPage *page)
{
    auto current_page = qobject_cast<InsetPage*>(stacked_widget_->currentWidget());
    auto current_idx = stacked_widget_->currentIndex();
    current_page->MadeHidden();
    stacked_widget_->removeWidget(current_page);
    current_page->setParent(nullptr);
    delete current_page;
    stacked_widget_->insertWidget(current_idx -1, page);
    stacked_widget_->setCurrentWidget(page);
    page->MadeVisible();
}

My question is, do I need to bother with reparenting the current page to a nullptr before deleting it, or can I just delete the current_page and the QStackedWidget will handle the fact that it's been deleted for me? I don't know if leaving the stacked widget as the parent but deleting the pointer will cause issues.

1

There are 1 best solutions below

0
Top-Master On

It depends on how the container-widget is implemented.

But in most-cases if not all, the container-widget (QStackedWidget in OP's case) updates own internal-state automatically once any child-widget is deleted.