Bahramdun Adil
Bahramdun Adil

Reputation: 6079

Qt object if created (newed) with 'this' needs to be deleted or automatically deleted?

I want to know whenever create any Qt object such as QTimer in heap with this keyword or parented with any other Qt objects, needs to be deleted or it will be deleted automatically by the parent?

Upvotes: 0

Views: 125

Answers (1)

dtech
dtech

Reputation: 49289

If your object is parented to an object tree, it will be collected when the tree root object is destroyed. You can still delete it before that if you need to.

QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor.

Upvotes: 3

Related Questions