PDA

View Full Version : Use of parent for automatic unload



PaceyIV
29th July 2009, 12:12
I've got a new little problem with the use of parent.

I create a new class based on QGridLayout.
In the constructor I create some new QWidget like label in this way


lbArray[N] = new QLabel(parent);

where parent is an argument of the constructor of my class:


IOPortConfig(QWidget *parent = 0);


In the MainWindow I create the new QWidget in this way:


d_IOInitConfig = new IOPortConfig(this);
hboxIOInitConfig->insertLayout(0, d_IOInitConfig);


hboxIOInitConfig is an Horizontal Layout partially created by the Designer.

The "problem" is that, in this way, I've got this warning:

QLayout: Attempting to add QLayout "" to MainWindow "MainWindow", which already has a layout

I want to use the parent so I don't need to worry about deleting the QWidget that I created in my class.

How should I do?

mcosta
29th July 2009, 13:09
Use QWidget::setLayout instead of create a new Layout with MainWindow as parent



hboxIOInitConfig = new QHBoxLayout;

....

this->setLayout(hboxIOInitConfig);

PaceyIV
29th July 2009, 14:03
I don't manually create the hboxIOInitConfig: it is created by the Designer.
Instead I create my component IOPortConfig and I pass to this Widget "this" as parent.

numbat
29th July 2009, 14:12
QMainWindow already has a layout (http://doc.trolltech.com/latest/qmainwindow.html#details). Instead create a widget to be your parent for other controls and add it to the main window layout using setCentralWidget.

PaceyIV
29th July 2009, 15:49
I don't understand.

New question, in the code below, IOPortConfig parent is hboxIOInitConfig?



d_IOInitConfig = new IOPortConfig(); // parent = NULL at this point
hboxIOInitConfig->insertLayout(0, d_IOInitConfig); // parent = hboxIOInitConfig?


If the answer is yes, Qt automatically can now delete all the Widget internal to IOPortConfig?

wysota
29th July 2009, 17:53
To cut this discussion... Any widget that is not a top-level window has a parent. If you don't give it a parent when creating it, it will be reparented when it's inserted into a layout. Either way it will be deleted when the window containing it is deleted.

PaceyIV
29th July 2009, 20:56
So, all the Widget that I create inside a class that inherit from QGridLayout they will be unload automatically when I delete the instance of the class without needed to manually remove in the destructor? And I don't need to force the parent when I create every widget. Is it right?

wysota
29th July 2009, 21:54
QGridLayout is not a widget and it's not practiced to subclass it to build a hierarchy of widgets (you should subclass QWidget for that) but yes, you don't need to destroy the widgets manually provided the layout is at some point applied to a widget.

PaceyIV
30th July 2009, 08:01
Well, why shouldn't be practiced?

I need to create a composite Widget with inside some array of QComboBox, QLabel, QLine edit. If I create a simple class inherit from QWidget, internally I need to create a QGridLayout and add all my controls to this. Instead I create a new class from QGridLayout and I only need to create the widgets and add these to my class.

wysota
30th July 2009, 08:13
Well, why shouldn't be practiced?
Because a layout can't live on its own, it's always enveloped in a widget.


I need to create a composite Widget with inside some array of QComboBox, QLabel, QLine edit. If I create a simple class inherit from QWidget, internally I need to create a QGridLayout and add all my controls to this. Instead I create a new class from QGridLayout and I only need to create the widgets and add these to my class.

Then there is a question whether it should be a class at all. I'd rather create a function that could be applied on any widget. Actually I wouldn't... I would subclass QWidget :)