PDA

View Full Version : Deleting children of a widget



ComServant
24th September 2011, 03:06
I have a QScrollArea and a QWidget in it, and I don't want to delete the widget, but I want to delete all the children and the layout in the widget. What is the official Qt way of doing that?

wysota
24th September 2011, 19:59
There is no official way. Just delete the objects you don't want.

33333
6th October 2011, 05:26
What? I am very new to Qt and am having trouble trying to understand the layout model (thats why I came here). But anyway from my initial research cant you do the following (C++) letting x be a widget with so called "children", y being a pointer to such a child widget:

x->layout()->removeWidget( y );

?

Added after 1 11 minutes:

Oh excuse me. I had assumed "layouts" were the only mechanism for adding "children" to a QWidget. But since QWidget extends QObject and QObject is some sort of node like object theres two mechanisms by which a QWidget can have children, or at least a type of descendants (Why did Qt feel the need make Qlayout a damn container then? Seems Bazaar...). And there does not appear to be any mechanisms for removing children directly. On reading the QObject docs it seems like the method suggested by wysota *is* the official method (http://doc.qt.nokia.com/4.7/qobject.html):

"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() 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() or findChildren()."

So you want to do: delete x->layout()

wysota
6th October 2011, 08:54
As far as I remember, deleting a layout doesn't remove child widgets from the widget since the layout is not the parent of child widgets inserted into the widget the layout manages.

33333
6th October 2011, 10:43
Ah yes now I see Im an idiot, you are correct wysota. I thought I have figured out the (insane IMHHHO) object model of Qt but I had not. Now I have.

Note (http://doc.qt.nokia.com/4.7/layout.html):


When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.

So even though Layouts can semantically and syntactically contain Widgets (void QLayout::addWidget ( QWidget * w )) those widgets are always (re)parented to the Widget that owns the given layout - if any. That is the relationship between a Layout and a widget is never parent child in the formal Qt sense.

And, Layouts can also contain layouts which can contain Layouts ... which can contain widgets. Such widgets are also (re)parented to the Widget that owns the ~top level Layout. However the contained layouts are actually children and descendants (in the formal Qt sense) of the Layout.

Yep insane.

Added after 8 minutes:



Yep insane. Then again got nothing on Java Swing!:)

wysota
7th October 2011, 00:09
There is nothing insane about Qt object model. I think you are overcomplicating things. Qt object model is a simple parent-child relationship of widgets, nothing more, nothing less. Layouts of widgets is a totally independent mechanism, with the only exception that adding a widget to a layout reparents it to the widget the layout manages which is compliant to a classic rule that a child widget occupies a subarea of its parent. You have one hierarchy of widgets (or actually objects) and a second hierarchy of layout items.