qt:heap_versus_stack
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
qt:heap_versus_stack [2010/12/08 22:11] – mithat | qt:heap_versus_stack [2010/12/08 22:59] (current) – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Heap or Stack? ====== | ====== Heap or Stack? ====== | ||
- | Should Qt objects be creates | + | Should Qt objects be created |
===== Short answer ===== | ===== Short answer ===== | ||
If the object is going to be part of a tree of Qt objects (as will be the case for most Qt GUI objects), then it should be created on the heap. | If the object is going to be part of a tree of Qt objects (as will be the case for most Qt GUI objects), then it should be created on the heap. | ||
- | If the object is not part of a tree or is the root of a tree, then it can be created either on the heap or the stack. It seems to be a tradition (or maybe there' | + | If the object is not part of a tree or is the root of a tree, then it can be created either on the heap or the stack. It seems to be a tradition (or maybe there' |
===== Long answer ===== | ===== Long answer ===== | ||
Line 14: | Line 14: | ||
For this to work, there are two requirements: | For this to work, there are two requirements: | ||
- | - All child objects must be created on the heap; however, the root itself may be on the stack. In other words, child objects that you intend to place in the tree must be created dynamically | + | - All child objects must be created on the heap; however, the root itself may be on the stack. In other words, child objects that you intend to place in the tree must be created dynamically using the '' |
- Child objects must explicitly be made children of their parents (i.e., they must be // | - Child objects must explicitly be made children of their parents (i.e., they must be // | ||
Line 20: | Line 20: | ||
<code cpp-qt> | <code cpp-qt> | ||
QObjectSubclass theParent; | QObjectSubclass theParent; | ||
- | + | AnotherQObjectSubclass *foo; // pointer to a child | |
- | AnotherQObjectSubclass *foo; | + | foo = new AnotherQObjectSubclass( "bar", & |
- | foo = new AnotherQObjectSubclass( "foo", & | + | |
</ | </ | ||
===== Automatic Management with Layout Managers ===== | ===== Automatic Management with Layout Managers ===== | ||
- | When widgets are added to [[layout managers]], they are automatically reparented to FIXME (the layout manager' | + | When widgets are added to [[layout managers]], they are automatically reparented to the overlying object |
<code cpp-qt> | <code cpp-qt> |
qt/heap_versus_stack.1291846265.txt.gz · Last modified: 2010/12/08 22:11 by mithat