qt - Using QObject instead of a container -
after reading on interesting parent-child system of qobject
wondering how common qt developers use in place of more traditional container. assuming memory contiguity not requirement, seems offers interesting features.
for example, have qobject
, give children of different types, , find children based on types, giving qobject dynamic heterogenous container-like feature, opposed required homogenous collection of traditional container.
and qobject
naturally manages memory of children, convenient well.
is common use of feature?
qobject::findchildren
slower storing objects in normal container qlist because:
- it iterates on children each time. searches recursively (but can disabled).
- it performs runtime type check.
- it constructs new qlist each time. can slow , expensive there many objects in result.
all above unnecessary if use qlist<type*> my_objects
. in case:
- you can name collection.
qlist<qpushbutton*> panic_buttons
clearerfindchildren<qpushbutton*>()
. - you can have several collections of objects of same type.
if want make heterogenous container, can use qhash<any_type_identifier, qobject*>
. faster.
maybe, findchildren approach may simplier sometimes. if have many objects or complicated class, you'd better use normal containers. can still use qobject's memory management them without problems.
Comments
Post a Comment