[Qt-interest] why parent widget in constructor?
Andreas Pakulat
apaku at gmx.de
Sun Apr 26 14:32:31 CEST 2009
On 26.04.09 14:05:48, 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?
In addition to what was said about the object tree's in Qt, note that
each and every QObject (and all subclasses, so also all QWidget
subclasses) use a default argument for the parent pointer. So creating a
QWidget without a parent and then adding it somewhere into an existing
widget hierarchy is very much possible. "QWidget* w = new QWidget;"
works just fine. In fact this is even used for one of the technologies
that QtGui provides, widgets in graphicsview. Here the widget that is
embedded is in fact a top-level widget, which is just moved to the right
place by the proxy-widget.
Andreas
--
Today is the tomorrow you worried about yesterday.
More information about the Qt-interest-old
mailing list