From 1e904ab342c1aaabbef67cbcc25cf3de9e35e755 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 26 Oct 2020 18:08:57 +0200 Subject: QLayout docs: explain better what the QWidget ctor arg does Make it clear in the docs that an alternative to calling QWidget::setLayout() is to pass the parent widget to the Q*BoxLayout constructor. This basically just copies the relevant bits from the the docs of QWidget and Q*Layout. Change-Id: Id196dcdf9a876d9141aa145f23a83c45f8cda5f8 Pick-to: 5.15 5.12 Reviewed-by: Richard Moe Gustavsen --- src/widgets/kernel/qboxlayout.cpp | 54 ++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 15 deletions(-) (limited to 'src/widgets/kernel/qboxlayout.cpp') diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp index b01227b8d5..5eaed16694 100644 --- a/src/widgets/kernel/qboxlayout.cpp +++ b/src/widgets/kernel/qboxlayout.cpp @@ -547,7 +547,11 @@ QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item) Constructs a new QBoxLayout with direction \a dir and parent widget \a parent. - \sa direction() + The layout is set directly as the top-level layout for \a parent. + There can be only one top-level layout for a widget. It is returned + by QWidget::layout(). + + \sa direction(), QWidget::setLayout() */ QBoxLayout::QBoxLayout(Direction dir, QWidget *parent) : QLayout(*new QBoxLayoutPrivate, nullptr, parent) @@ -1231,11 +1235,16 @@ QBoxLayout::Direction QBoxLayout::direction() const \snippet layouts/layouts.cpp 4 \snippet layouts/layouts.cpp 5 - First, we create the widgets we want in the layout. Then, we - create the QHBoxLayout object and add the widgets into the - layout. Finally, we call QWidget::setLayout() to install the - QHBoxLayout object onto the widget. At that point, the widgets in - the layout are reparented to have \c window as their parent. + First, we create the widgets we want to add to the layout. Then, + we create the QHBoxLayout object, setting \c window as parent by + passing it in the constructor; next we add the widgets to the + layout. \c window will be the parent of the widgets that are + added to the layout. + + If you don't pass parent \c window in the constrcutor, you can + at a later point use QWidget::setLayout() to install the QHBoxLayout + object onto \c window. At that point, the widgets in the layout are + reparented to have \c window as their parent. \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets @@ -1244,8 +1253,13 @@ QBoxLayout::Direction QBoxLayout::direction() const /*! - Constructs a new top-level horizontal box with - parent \a parent. + Constructs a new top-level horizontal box with parent \a parent. + + The layout is set directly as the top-level layout for \a parent. + There can be only one top-level layout for a widget. It is returned + by QWidget::layout(). + + \sa QWidget::setLayout() */ QHBoxLayout::QHBoxLayout(QWidget *parent) : QBoxLayout(LeftToRight, parent) @@ -1294,11 +1308,16 @@ QHBoxLayout::~QHBoxLayout() \snippet layouts/layouts.cpp 10 \snippet layouts/layouts.cpp 11 - First, we create the widgets we want in the layout. Then, we - create the QVBoxLayout object and add the widgets into the - layout. Finally, we call QWidget::setLayout() to install the - QVBoxLayout object onto the widget. At that point, the widgets in - the layout are reparented to have \c window as their parent. + First, we create the widgets we want to add to the layout. Then, + we create the QVBoxLayout object, setting \c window as parent by + passing it in the constructor; next we add the widgets to the + layout. \c window will be the parent of the widgets that are + added to the layout. + + If you don't pass parent \c window in the constrcutor, you can + at a later point use QWidget::setLayout() to install the QVBoxLayout + object onto \c window. At that point, the widgets in the layout are + reparented to have \c window as their parent. \image qvboxlayout-with-5-children.png Horizontal box layout with five child widgets @@ -1306,8 +1325,13 @@ QHBoxLayout::~QHBoxLayout() */ /*! - Constructs a new top-level vertical box with - parent \a parent. + Constructs a new top-level vertical box with parent \a parent. + + The layout is set directly as the top-level layout for \a parent. + There can be only one top-level layout for a widget. It is returned + by QWidget::layout(). + + \sa QWidget::setLayout() */ QVBoxLayout::QVBoxLayout(QWidget *parent) : QBoxLayout(TopToBottom, parent) -- cgit v1.2.3