1. If a class is derived from QObject, then usually you create it on the heap using new. If you give the new instance a parent, then the parent will delete it and there is no memory leak. You can give an object a parent in many ways:
- in the constructor: myObject = new QObject( parentObject );
- explicitly: myObject->setParent( parentObject );
- using some other method call that internally sets the parent: parentObject->addWidget( myWidget );, etc.
Classes derived from QObject always have a constructor that take a "parent" argument.
2. If a class is not derived from QObject (QColor, QFont, QPen, QString, etc.), you almost always create it on the stack: QPen myPen; and pass it into a method by reference: painter->setPen( myPen ); If you create the instance on the heap using new, then you must also delete it.
Bookmarks