first off, this is really easy to find out on your own -- write your own class derived from QObject and put traces in the destructor to see if it ever gets deleted.
secondly, "no" -- objects of type QObject do not delete themselves automatically unless they are child objects of a parent object also derived from QObject. Whoever told you this is wrong -- or you misunderstood what they were trying to tell you.
---
an example...
QObject *object1 = new QObject;
QObject *object2 = new QObject(object1);
To copy to clipboard, switch view to plain text mode
...object2 does not need to be deleted explicitly because object1 is its parent -- but object1 does need to be deleted explicitly because it has no parent.
So when you call "delete" on object1, object2 is also deleted. Conversely, you should NOT call "delete" on object2 unless you remove it from object1's ownership tree.
OTOH if you did this...
QObject *object1 = new QObject;
QObject *object2 = new QObject;
To copy to clipboard, switch view to plain text mode
...then BOTH of them need to be deleted explicitly since neither one has a parent specified in the constructor.
---
so to be perfectly clear, going back to the first example...
[...] // do what you will with object1 and object2...
delete object1;
[...] // ...don't use object1 and object2 because they're both gone.
QObject *object1 = new QObject;
QObject *object2 = new QObject(object1);
[...] // do what you will with object1 and object2...
delete object1;
[...] // ...don't use object1 and object2 because they're both gone.
To copy to clipboard, switch view to plain text mode
---
Since most QObjects (if not all of them) in your apps will be created after the main QApplication object is allocated, you can use the QApplication object as the first parent so you never have orphaned objects derived from QObject...
another example (given "MyClassFactory" and "MyClass" both derive from QObject)...
MyClass *MyClassFactory::createMyClass()
{
return new MyClass(this); // sets myFactory as myClass's parent in "main"...
}
int main(int argc, char **argv)
{
// init app...
MyClassFactory *myFactory = new MyClassFactory(myApp);
MyClass *myClass1 = myFactory->createMyClass();
MyClass *myClass2 = myFactory->createMyClass();
// myClass3, myClass4, etc, etc...
// run app...
app.exec();
// cleanup app...
/* deleting myApp also deletes myFactory and every object created with MyClassFactory::createMyClass() */
delete myApp;
//... done
return 0;
}
MyClass *MyClassFactory::createMyClass()
{
return new MyClass(this); // sets myFactory as myClass's parent in "main"...
}
int main(int argc, char **argv)
{
// init app...
QApplication *myApp = new QApplication(argc, argv);
MyClassFactory *myFactory = new MyClassFactory(myApp);
MyClass *myClass1 = myFactory->createMyClass();
MyClass *myClass2 = myFactory->createMyClass();
// myClass3, myClass4, etc, etc...
// run app...
app.exec();
// cleanup app...
/* deleting myApp also deletes myFactory and every object created with MyClassFactory::createMyClass() */
delete myApp;
//... done
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks