PDA

View Full Version : What owns what?



cass
11th May 2010, 10:05
I know QT has a parent/child hierarchy way of storing QObject's.


But what happens to root QWidgets? They do not have a constructor that takes a QObject parent. It is possible to make a QObject that does not inherit QWidget the parent of a QWidget?
What happens if you add a QWidget to a QScene? Does the QScene now owns the QWidget or does the QGraphicsViewProxy now owns the QWidget?
What owns the QGraphicsViewProxy objects that is returned by scene->addWidget(new_widget)?
Will a widget take ownership of a layout? E.g. what happens with root_widget->setLayout(new_layout)?
Will the layout take ownership if I add a widget/layout to a root layout, e.g. root_layout->addWidget(new_widget) or root_layout->addLayout(new_sub_layout)?
If I have an application that dynamically creates objects and setup QT signal connections between the events with QObject::connect(), is it safe to assume that the connections will automatically be deleted when either one of the objects they used to connect gets deleted?

Zlatomir
11th May 2010, 10:34
1) "Root" QWidget is the widget that don't have a parent at construction if you don't set parameter for parent it is default 0, witch means the widget is a window/used for "parenting" other widgets.

2) Not sure what you meant. Why you want a simple QWidget in anything else? A QWidget has only basic stuff for containing other widgets (derived from QWidget and implementing some functionality)

3) I don't know the QGraphicsViewProxy, but basic rule is: if you add a widget to some container it will be auto-parented to that.

4/5) No, the layout is not really implicated in parent/child relation:


QVBoxLayout *lay = new QVBoxLayout(window);

This means that if you add some widgets to the layout that widgets will become child-widgets of the "window" widget.

6) I don't know, but why do you want some of that? It is your job to make sure that widgets that "need" each-other will be "alive" in the same time, that is the reason why we need to dynamically allocate widgets on the heap (using pointers and new).

wysota
11th May 2010, 15:07
But what happens to root QWidgets?
Nothing happens to them.

What happens if you add a QWidget to a QScene?
Does the QScene now owns the QWidget or does the QGraphicsViewProxy now owns the QWidget?
Reading the docs from time to time, really doesn't hurt.

QGraphicsProxyWidget shares ownership with QWidget, so if either of the two widgets are destroyed, the other widget will be automatically destroyed as well.

What owns the QGraphicsViewProxy objects that is returned by scene->addWidget(new_widget)?
There is no such class in Qt currently but if you mean QGraphicsProxyWidget then it's an item like any other so the same rules that apply to other items apply to this item class as well.

Will a widget take ownership of a layout?
Yes, the layout is reparented to the widget it controls.

E.g. what happens with root_widget->setLayout(new_layout)?
What do you mean "what happens"? The world will not explode, I can tell you that :)


Will the layout take ownership if I add a widget/layout to a root layout, e.g. root_layout->addWidget(new_widget) or root_layout->addLayout(new_sub_layout)?
Layout doesn't own any widgets.


If I have an application that dynamically creates objects and setup QT signal connections between the events with QObject::connect(), is it safe to assume that the connections will automatically be deleted when either one of the objects they used to connect gets deleted?

Yes.

You know, all of these questions could have been answered by writing a simple test application and it would take less time than asking those questions here...