[Qt-interest] why parent widget in constructor?

Donal O'Connor donaloconnor at gmail.com
Sun Apr 26 14:12:44 CEST 2009


*QObjects organize themselves in object trees. When you create a QObject
with another object as parent, the object will automatically add itself to
the parent's children <http://doc.trolltech.com/4.5/qobject.html#children>()
list. The parent takes ownership of the object i.e. it will automatically
delete its children in its destructor. You can look for an object by name
and optionally type using
findChild<http://doc.trolltech.com/4.5/qobject.html#findChild>()
or findChildren <http://doc.trolltech.com/4.5/qobject.html#findChildren>().*

Java has garbage collection, but in C++ something must take responsibility
for deleting objects.


On Sun, Apr 26, 2009 at 1:05 PM, <fluca1978 at infinito.it> wrote:

> Hi all,
> I've got a question about the adoption of a "parent" widget pointer in the
> constructors of almost all widgets. I mean, this way when I want to build
> up
> a new widget, say a QPushButton, I have to specify which widget it will be
> contained into. Other GUI toolkit, such as Swing, do something different:
> GUI blocks are built and then connected together (e.g., using an add method
> on the container).
> Now, it is clear that the QT approach has one immediate advantage: it
> allows
> a widget to know if it is a top level window widget or not. However I'm
> wondering if there's any other tecnhical reason behind this approach. Any
> clue?
>
> Moreover, another doubt I've got is this: the parent widget dominates the
> display and behaviour of child widgets, meaning that if for instance the
> parent widget is not visible, neither the child will be. Now, in the QT
> approach the child widget must handle a pointer to its parent, but I guess
> that internally something must be set within the parent widget to point to
> its child, otherwise how could a child know when it is time to show up or
> not? Such information is surely known to the parent widget, but if the
> parent widget has no pointer to the child one, how could it know? Again,
> any
> clue or source pointers are welcome.
>
> Thanks,
> Luca
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090426/69a0a13f/attachment.html 


More information about the Qt-interest-old mailing list