How pointers to widgets are handled?
In Qt doc examples, like tabdialog.pro example.
There are some member (pointer) of Qt widgets like tabWidget
defined in tabdialog.h
used in tabdialog.cpp
@tabWidget = new QTabWidget;
since no parent are set for these widgets, how/when are they deleted?
A parent is either set explicitly (parent passed to constructor, QObject::setParent()) or implicitly, for example when widgets are added to a layout or other widgets.
Good observation there. But if you dig a bit further you will notice
When you look in the documentation of the QBoxLayout, it doesn't mention anything about ownership.. but if you dig further you will reach the addItem virtual function of QLayout.. where it is noted..
bq. Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
and the addWidget in QLayout notes
bq. Adds widget w to this layout in a manner specific to the layout. This function uses addItem().
Ofcourse, the documentation doesn't specify explicitly that the QBoxLayout::addWidget uses the QLayout::addWidget.. but its a fairly valid assumption. The QBoxLayout implements addItem and should manage the memory mangement as defined by the QLayout interface. I can try to confirm this.. but maybe some trolls could intervene..
Thanks Jim, liked your elaboration on the subject.
Replies are quite satisfactory,
So?!?! Who has the ownership to the widget?? It was not answered.
The widget is owned by it's parent, whether the parent is set explicitly or by proxy of a layout.
Ok, I didn't actually have the time to extend QWidget and overwrite the destructor to put a qDebug() and see that it's being called, but I guess when using QHBoxLayout::addWidget() the layout takes the ownership of the added widget. The documentation should be more clear and specifically state this, without saying addWidget() uses addItem() and one has to go to addItem() and deduct that addWidget() takes ownership (because addItem() takes ownership).
Well, if you think that, please go ahead and:
Report a bug against the documentation, or even better:
Update the documentation yourself, and submit it via Gerrit.
Qt is a community effort, and you're part of that community! You can make a difference. If you just post your griefs in a forum topic, chances are slim to none that they will be picked up and acted upon.
[quote author="deion" date="1331894073"]Ok, I didn't actually have the time to extend QWidget and overwrite the destructor to put a qDebug() and see that it's being called, but I guess when using QHBoxLayout::addWidget() the layout takes the ownership of the added widget.[/quote]
Be aware that ownership and parentship are two different things.
If you add a widget to a layout, the layout becomes the owner of the widget, but it does not become the parent (only widgets can be a parent of a widget, and a layout is not a widget). The parent widget of the layout takes parentship, the layout takes ownership.
This also implies that widgets, which are added to a layout without a parent widget will leak memory as they are not deleted automatically due to having no parent.
I understand your point Andre, but there is one issue with this approach (maybe this is common to all community projects): I have to invest quite some time to follow the official upstream procedures; Complaining on forums actually DOES help :) I got to this thread by google-ing for "QBoxLayout addWidget ownership", so next time someone has the same question, there is a big chance he will end-up on this thread and see the discussion; so maybe it's not as helpful as formally reporting a bug/updating the documentation but it's still something.
PLUS: Complaining is FUN, following procedures is kind of boring :D