Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of QWidget's parent?

Tags:

c++

qt

If I subclassed any widget the usual pattern is:

ZTabWidget::ZTabWidget(QWidget *parent):QTabWidget(parent){
  blah ... blah ...
}

The usual pattern is:

WidgetB widgetb = new WidgetB(widgeta)
widgeta.addWidget(widgetb);

Is there any harm in having all my widgets assigned MainWindow as their respective parent - even though, following the trails of addWidget hierarchy, most of those widgets have another widget as the addWidget parent:

WidgetB widgetb = new WidgetB(mainWindow)
widgeta.addWidget(widgetb);

The reason I wish to do this is because mainWindow holds all the main items of the application and that I could have my any of my subclassed widgets reference those items.

In fact, I am thinking of a proxy widget that is merely used to hold some common references and use that proxy widget as the common parent of all my widgets. Any problem in that?

Exactly, what is the use of having registered a parent for a QWidget? Is the parent even used internally?

Does the addWidget parent have to be the same as the constructor parent?

like image 355
Blessed Geek Avatar asked Jan 10 '12 03:01

Blessed Geek


People also ask

What is a QWidget?

The QWidget class is the base class of all user interface objects. The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order.

What is Qt size hint?

size hint is the preferred size of the widget, layouts will try to keep it as close to this as possible. size policy describes how the size may change when the preferred size cannot be used (can it stretch or shrink) see the QSizePolicy::Policy enum for a description of each.


1 Answers

There are actually two aspects two your questions:

Memory management

The tree-like organization of QWidgets (and in fact of any QObjects) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted. From the docs:

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().

This means you can organize your widgets in any way that seems sensible to you (as long as you don't introduce memory leaks). I think, however, that it is a common practice to assign all the widgets to their respective container (either explicitly, or better, using methods like addWidget).

If a widget is assigned to a QLayout using addWidget, then ownership of the widget is transferred to the layout (which in turn is probably owned by another surrounding layout or the main window). So yes, the relationship defined by this method includes the more general parent-child relationship described above.

Now, as soon as the main window gets destroyed, basically the whole tree of QObjects is deleted as you'd expect.

Consequently, if you leave an object "parentless", it is your own responsibility to delete it.

GUI semantics

As Frank noted correctly, in some contexts the parent-child relationship between QWidgets also bears a semantically meaning to the GUI framework. An example of this are modal dialogs, who block their parent as long as they stay open.

like image 161
Niklas B. Avatar answered Oct 16 '22 14:10

Niklas B.