Basically it depends upon how you create the object. It's not just sufficient to have a class inherit from a QObject for it to be managed by Qt. If you create an object and ensure that it has a parent then when the parent is deleted it will delete it's children.
So in the example you gave mainWidget has the parent app, so when the application is closed then mainWidget will be freed. labelChild has the Widget as it's parent so when Widget is deleted it will ensure that labelChild is properly deleted. However, your pushButtons are not assigned any parent and so they will not be tidied up.
You should see that your code as posted needs some modification but the changes are trivial. As a help put a qDebug() statement in your destructors, just so that you understand what is going on and that you are taking advantage of the auto deletion capabilities of Qt




Reply With Quote
Bookmarks