It's not that straight-forward at all. It's perfectly valid to allocate for example the main window in main() and modal dialogs on the stack. Why? Both are QWidgets.
QObjects organize themselves in object trees. A parent takes ownership of its children and automatically deletes them when the parent is deleted. This would cause problems if children were allocated on the stack because they would (depending on the allocation order) end up being deleted twice; 1) by their parent and 2) by normal C++ rules when going out of scope. However, neither QPixmap, QPalette nor other implicitly shared classes are QObjects.
Bookmarks