setLayout to a widget Vs setting parent to the layout
-
Hi All,
I want to know the difference between setting a layout to a widget and setting a parent to a layout. Please refer to the following codes to get a clarity to my points:
CASE-1
: Setting layout to a widgetQWidget *parentWidget= new QWidget(); FlowLayout *flowL = new FlowLayout(); ... ...... .............. parentWidget->setLayout(flowL);
CASE-2
setting parent to a layoutQWidget *parentWidget= new QWidget(); FlowLayout *flowL = new FlowLayout(parentWidget); ..... ......... ............. parentWidget->setLayout(flowL);
-
@Swati777999 CASE-1 and CASE-2 are equivalent. Creating a QLayout with a specified QWidget parent automatically sets it as the QWidget's layout. In CASE-1 you have to set the layout on the parent widget manually because the layout is parentless. In CASE-2 the call to setLayout() is unnecessary because the layout constructor did it for you (it does no harm to call it again).
@AnneRanch jsulm's response was simply asking if the OP had read the manual and pointing at the relevant bit. The information is in the manual and reasonably easy to find, but it is possible the OP read it and did not understand. As for a "code example"... the example is the original post.
-
@Swati777999 Did you read documentation which explains this?
https://doc.qt.io/qt-5/qwidget.html#QWidget
https://doc.qt.io/qt-5/qlayout.html#QLayout -
Ah... the well known "RTFM" answer style (is back ).
Can you come up with a simple code example ? Or reference to example?
I have been struggling with similar in "area" usage . -
@Swati777999 CASE-1 and CASE-2 are equivalent. Creating a QLayout with a specified QWidget parent automatically sets it as the QWidget's layout. In CASE-1 you have to set the layout on the parent widget manually because the layout is parentless. In CASE-2 the call to setLayout() is unnecessary because the layout constructor did it for you (it does no harm to call it again).
@AnneRanch jsulm's response was simply asking if the OP had read the manual and pointing at the relevant bit. The information is in the manual and reasonably easy to find, but it is possible the OP read it and did not understand. As for a "code example"... the example is the original post.