Arrange widgets properly on a layout
-
Thanks all,
The QFormLayout looks nice and applicable, with much less code:
#include "dateofhire.h" #include <QLineEdit> #include <QDateEdit> #include <QPushButton> #include <QFormLayout> DateOfHire::DateOfHire(QWidget *parent) : QDialog(parent) { nameLineEdit = new QLineEdit; titleLineEdit = new QLineEdit; hireDateEdit = new QDateEdit; ok = new QPushButton(tr("&OK")); QFormLayout* form = new QFormLayout; form->addRow(tr("Name"), nameLineEdit); form->addRow(tr("Title"), titleLineEdit); form->addRow(tr("Hire"), hireDateEdit); form->addWidget(ok); setLayout(form); }
The result looks:
While I need some Stretch for the button to look:
@J-Hilk: Do you mean to use Q[V/H]BoxLayouts but add stretch for the layouts and the button, please? I only use pure code not designer.
@tomy said in Arrange widgets properly on a layout:
@J-Hilk: Do you mean to use Q[V/H]BoxLayouts but add stretch for the layouts and the button, please? I only use pure code not designer.
I meant
https://doc.qt.io/qt-5/qboxlayout.html#setStretchmyLayout->setStretch(0,1);
myLayout->setStretch(1,2);@JonB It's not entirely style only.
Reparenting will cause a chain of recalculating in the layout and other stuff. It's usually more efficient so create layout items without a parent ;)
-
@tomy said in Arrange widgets properly on a layout:
@J-Hilk: Do you mean to use Q[V/H]BoxLayouts but add stretch for the layouts and the button, please? I only use pure code not designer.
I meant
https://doc.qt.io/qt-5/qboxlayout.html#setStretchmyLayout->setStretch(0,1);
myLayout->setStretch(1,2);@JonB It's not entirely style only.
Reparenting will cause a chain of recalculating in the layout and other stuff. It's usually more efficient so create layout items without a parent ;)
@J-Hilk said in Arrange widgets properly on a layout:
@JonB It's not entirely style only.
Reparenting will cause a chain of recalculating in the layout and other stuff. It's usually more efficient so create layout items without a parent ;)I don't understand. In the code @VRonin pasted, what reparenting will be going on?
nameLineEdit = new QLineEdit(this); form->addRow(tr("Name"), nameLineEdit);
After the second line, how will the parent be any different from the
this
he passes as parent in the first line? -
@J-Hilk said in Arrange widgets properly on a layout:
@JonB It's not entirely style only.
Reparenting will cause a chain of recalculating in the layout and other stuff. It's usually more efficient so create layout items without a parent ;)I don't understand. In the code @VRonin pasted, what reparenting will be going on?
nameLineEdit = new QLineEdit(this); form->addRow(tr("Name"), nameLineEdit);
After the second line, how will the parent be any different from the
this
he passes as parent in the first line?@JonB from here
https://doc.qt.io/archives/qt-4.8/layout.htmlTips for Using Layouts
When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.
Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.
You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.
so, you're right in this example, the overall parent will be the widget that was the parent for the lineEdit, but only at the end.
The Layout does not have an owner in the beginning, so I assume(haven't checked) it will reparent to a null ptr and then reparent the the original parent, when it's assigned to a widget
-
@tomy said in Arrange widgets properly on a layout:
@J-Hilk: Do you mean to use Q[V/H]BoxLayouts but add stretch for the layouts and the button, please? I only use pure code not designer.
I meant
https://doc.qt.io/qt-5/qboxlayout.html#setStretchmyLayout->setStretch(0,1);
myLayout->setStretch(1,2);@JonB It's not entirely style only.
Reparenting will cause a chain of recalculating in the layout and other stuff. It's usually more efficient so create layout items without a parent ;)
@J-Hilk said in Arrange widgets properly on a layout:
Reparenting will cause a chain of recalculating in the layout and other stuff. It's usually more efficient so create layout items without a parent ;)
Layouts are QObjects, not QWidgets so they don't own widgets.
The parent of a widget will always be a widget so in my code above there is no reparenting going on at all.
Passing the parent in the constructor is just one of those things that sometimes don't do anything but you never want to forget when it's needed, hence I always parent QObjects on construction when I can. -
@JonB from here
https://doc.qt.io/archives/qt-4.8/layout.htmlTips for Using Layouts
When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.
Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.
You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.
so, you're right in this example, the overall parent will be the widget that was the parent for the lineEdit, but only at the end.
The Layout does not have an owner in the beginning, so I assume(haven't checked) it will reparent to a null ptr and then reparent the the original parent, when it's assigned to a widget
@J-Hilk said in Arrange widgets properly on a layout:
The Layout does not have an owner in the beginning, so I assume(haven't checked) it will reparent to a null ptr and then reparent the the original parent, when it's assigned to a widget
Hmm. So you're saying when he goes
form->addRow(tr("Name"), nameLineEdit);
that will unparent the widget, and only later reset it to the originalthis
parent when he later ultimately goessetLayout(mainLayout);
causing everything to reparent, is that right?EDIT I see @VRonin has just come to hos own defence! Saying what I said. Clearly this needs investigation if I really want to know what is going on....
-
@J-Hilk said in Arrange widgets properly on a layout:
The Layout does not have an owner in the beginning, so I assume(haven't checked) it will reparent to a null ptr and then reparent the the original parent, when it's assigned to a widget
Hmm. So you're saying when he goes
form->addRow(tr("Name"), nameLineEdit);
that will unparent the widget, and only later reset it to the originalthis
parent when he later ultimately goessetLayout(mainLayout);
causing everything to reparent, is that right?EDIT I see @VRonin has just come to hos own defence! Saying what I said. Clearly this needs investigation if I really want to know what is going on....
@JonB said in Arrange widgets properly on a layout:
So you're saying when he goes form->addRow(tr("Name"), nameLineEdit); that will unparent the widget, and only later reset it to the original this parent when he later ultimately goes setLayout(mainLayout); causing everything to reparent, is that right?
Nope. If the layout is not set on a widget it will not change parenting of widgets added to it. Source: https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qlayout.cpp.html#_ZN7QLayout14addChildWidgetEP7QWidget
-
@JonB said in Arrange widgets properly on a layout:
So you're saying when he goes form->addRow(tr("Name"), nameLineEdit); that will unparent the widget, and only later reset it to the original this parent when he later ultimately goes setLayout(mainLayout); causing everything to reparent, is that right?
Nope. If the layout is not set on a widget it will not change parenting of widgets added to it. Source: https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qlayout.cpp.html#_ZN7QLayout14addChildWidgetEP7QWidget
@VRonin said in Arrange widgets properly on a layout:
Nope. If the layout is not set on a widget it will not change parenting of widgets added to it.
That's what I thought! Now I don't know whom to believe... ;-) I respect both you & @J-Hilk , I'm torn....
-
I said I assume ;) it reparents to nullptr
so clearly I was wrong 🙈
-
I tend to use parenting using the layouts and find it better and nicer (less verbose).
But how could "exiting the method early" happen in an example, please?I tend to use parenting using the layouts and find it better and nicer (less verbose).
it's just personal preference, both methods are basically equivalent
But how could "exiting the method early" happen in an example, please?
for example if something throws before you set the layout you leak memory. not a problem in this code
-
I tend to use parenting using the layouts and find it better and nicer (less verbose).
But how could "exiting the method early" happen in an example, please?@tomy said in Arrange widgets properly on a layout:
But how could "exiting the method early" happen in an example, please?
Apart from throwing, if a developer puts in e.g. a
if (...) return
statement. Or anif
generally, and a widget is left unparented, for whatever reason. You & I might notice, someone else might not :) Just saying. -
@tomy said in Arrange widgets properly on a layout:
But how could "exiting the method early" happen in an example, please?
Apart from throwing, if a developer puts in e.g. a
if (...) return
statement. Or anif
generally, and a widget is left unparented, for whatever reason. You & I might notice, someone else might not :) Just saying.