views:

284

answers:

3

Hi, I am using QStackedWidget to switch between the views, I will be able to traverse between the views, I am facing problem in some scenario, where I do not require the widgets, I want to remove them completely.. QStackedWidget provides the functionality to remove widgets, still the ownership of the widget will be there, I mean widget will be hidden.

I dont want this to be happen, when I call remove widget the widget should be deleted. how to handle this?

A: 

If you add a real object to the stack, it's destructor should be called when removed. If you add a pointer to the stack, then on removal call delete on the pointer.

Note that Qt Objects do reference counting.

ypnos
Hey thanks,but when i call the remove its destructor is not calling.i will try by deleting widget
Shadow
+2  A: 

Have you tried simply deleting the object after removing it? i.e.

stackWidget->removeWidget(myWidget);
delete myWidget;
Mike Mueller
Thanks..i will check
Shadow
The documentation of QStackedWidget::removeWidget is confusing: ownership _does_ transfer back to the application, and it's correct to delete/free the widget after calling removeWidget.
BruceCran
A: 

Many Qt objects will get automatically removed when deleted. (At least, in the QtGraphics API things work that way) I suspect that simply deleting the object will also remove it, since Qt Objects do emit a signal that they have been deleted.

Arcane
you think, i have a member variable or local variable, i will assign memory dynamically, in this case when i delete object those members and local variable deleted automatically? or we need to do manual delete?
Shadow