QLayout attempting to add qlayout
-
My hierarchy is the following.
A Dialog contains Vertical Layout
Vertical Layout contains a Tab Widget and a Horizontal Layout
The Horizontal Layout contains a label and a textbox.My goal is to ensure that right parenting is done to the controls and the layouts so that I donot have to clean-up objects/garbage collect the controls created with
new
The code is of the following dialog = new QDialog(); dialog_vertical_layout = new QVBoxLayout(dialog); tabWidget = new QTabWidget(dialog); executeMetadataLayout=new QHBoxLayout(dialog); execute_button=new QPushButton(dialog); execute_button->setText(QString("Execute")); dialog_vertical_layout->addWidget(tabWidget); dialog_vertical_layout->addLayout(executeMetadataLayout); dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->show();
I am getting warnings with QLayout attempting to add qlayout what is going wrong?
Also to achieve my goals am I in the right direction? -
@gully said in QLayout attempting to add qlayout:
executeMetadataLayout=new QHBoxLayout(dialog);
You're creating a second layout with dialog as parent. Since this ctor automatically adds the layout to the widget you'll get an error because there is already a layout set.
I don't see a reason for the second layout since you've only two widgets which perfectly fit into the first QVBoxLayout.
Also I would use Qt designer instead creating the ui elements by hand. -
@Christian-Ehrlicher I am setting the parent so that I donot have to do garbage collection separately. The parent child relationship ensures that the child objects (created with new especially) are deleted once the parent is destroyed.
How can I in this case maintain automatic garbage collection and set the layouts at the same time?
-
You can only set one layout to one widget at the same time (therefore the warning). As I said - I don't see what's the second layout is for in your example. Simply use the Qt designer for such stuff and you're fine.
-
@Christian-Ehrlicher using QT designer is just going to help me run away from this problem rather than solving it. I have my reasons not to use QT Designer.
I understand where I am going wrong. I know how to solve it as well. What I donot understand is how can I create layout as well as maintain the parent-child relationship so that automatic garbage collection is ensured
-
Since a widget can't have two layouts I don't see a reason why you need a second layout with this widget as parent. Just use the widget where the layout is used for (which is done implicit in the ctor or with widget->setLayout())
-
@Christian-Ehrlicher just looking for your confirmation/rejection here , if I do setWidget() or setLayout does these function automatically set parent child relationship- so that the objects created on heap are not required to be garbage collection manually/writing code for it ?
-
@Christian-Ehrlicher just looking for your confirmation/rejection here , if I do setWidget() or setLayout does these function automatically set parent child relationship- so that the objects created on heap are not required to be garbage collection manually/writing code for it ?
@gully just use the help: http://doc.qt.io/qt-5/qlayout.html#addItem which is called from
addWidget
, takes ownership of the added widget. Thats what you want.