[SOLVED] When Qt automatically parents my objects?
-
Hello,
i am aware of the Qt's child parent relationship and the automatically memory deallocation.
In some cases like these@QWidget::setlayout()
QTableWidget::setItem()
QBoxLayout::addLayout@Qt automatically parents my widgets and i won't have to worry about memory leaks.
In general how can i know which methods behaves similar to those three above?thanks in advance
-
Most of the time straight from the documentation. In general, you can assume that every time you pass a pointer to an object which is meant to be "hold" somehow, there's a ownership transfer involved. A notable exception is QLayout::addWidget (which does NOT reparent the widget to the layout), and probably there are some others (documented or not; of course, the source code has the final word).
-
[quote author="peppe" date="1314039208"]A notable exception is QLayout::addWidget (which does NOT reparent the widget to the layout).[/quote]
The parent widget of the layout becomes the new parent for the widget.
[quote author="peppe" date="1314039208"](documented or not; of course, the source code has the final word).[/quote]
"http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/kernel/qlayout.cpp#line989":http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/kernel/qlayout.cpp#line989 ;-)
-
Lukas, are you sure ? If the QWidget already has a parent, it still moves to the correct parrent, see
@
void QLayout::addChildWidget(QWidget *w)if (pw && mw && pw != mw) {
#ifdef QT_DEBUG
if (layoutDebug())
qWarning("QLayout::addChildWidget: %s "%s" in wrong parent; moved to correct parent", w->metaObject()->className(), w->objectName().toLocal8Bit().data());
#endif
pw = 0;
@EDIT: ok, we agree on this point :)
-
[quote author="Lukas Geyer" date="1314043098"][quote author="peppe" date="1314039208"]A notable exception is QLayout::addWidget (which does NOT reparent the widget to the layout).[/quote]
The parent widget of the layout becomes the new parent for the widget.
[quote author="peppe" date="1314039208"](documented or not; of course, the source code has the final word).[/quote]
"http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/kernel/qlayout.cpp#line989":http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/kernel/qlayout.cpp#line989 ;-)[/quote]
So what? That's exactly what I was saying. The layout is does not reparent the widget to itself.
-
It's the general rule. Cf. the documentation of the methods you mentioned at the beginning:
bq. The QWidget will take ownership of layout.
bq. The table takes ownership of the item.
QBoxLayout::addLayout documentation says nothing about that, but still performs the ownership transfer (layouts form a QObject hierarchy -- the widgets they manage don't, and they're all children of the widget the top-level layout is applied upon).
-
[quote author="peppe" date="1314044925"]So what? That's exactly what I was saying. The layout is does not reparent the widget to itself.[/quote]
That's absolutely true :-) But one might interpret this paragraph as that QLayout::addWidget() is an exception to the list of methods which reparent a widget, which is not true.
So yes, QLayout::addWidget() does not reparent the widget to the layout, but it does reparent your widget - to the parent widget of the layout. A widget added to a layout of another widget will be automatically deleted as soon as the parent widget is deleted.
-
[quote author="octal" date="1314043459"]Lukas, are you sure ? If the QWidget already has a parent, it still moves to the correct parrent.[/quote]
Widget A is added to the layout of widget B.
- If B is already the parent of A nothings happens.
- If A has another parent than B, B becomes the new parent for A.
- If A has no parent, B becomes the new parent for A.
-
No, but see "here":http://developer.qt.nokia.com/forums/viewthread/9050/.
-
[quote author="Lukas Geyer" date="1314266752"]No, but see "here":http://developer.qt.nokia.com/forums/viewthread/9050/.[/quote]
That links to this thread - a mistake?