gfree
gfree

Reputation: 507

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

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.

Upvotes: 1

Views: 279

Answers (1)

Top-Master
Top-Master

Reputation: 8735

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.

Upvotes: 0

Related Questions