Layouts docs: pass parent widget in the ctor

This is a follow up from commit 1e904ab342c1aaa; changing more
documentation to pass a widget * in the ctor of a layout, rather
than creating a parent-less layout then calling setLayout().

Change-Id: I4fc59c6cfa46ccd279a153acd67335a6daf22ff9
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
This commit is contained in:
Ahmad Samir 2020-11-10 20:30:49 +02:00
parent 77eef32917
commit d60aa6a8cf
12 changed files with 35 additions and 39 deletions

View File

@ -31,14 +31,16 @@ int main(int argc, char *argv[])
qDebug() << fileInfo5.fileName();
qDebug() << fileInfo6.fileName();
QGroupBox *groupBox = new QGroupBox(QStringLiteral("QFileInfo::dir() test"));
QVBoxLayout *vbox = new QVBoxLayout(groupBox);
QPushButton* button1 = new QPushButton(fileInfo1.dir().path());
QPushButton* button2 = new QPushButton(fileInfo2.dir().path());
QPushButton* button3 = new QPushButton(fileInfo3.dir().path());
QPushButton* button4 = new QPushButton(fileInfo4.dir().path());
QPushButton* button5 = new QPushButton(fileInfo5.dir().path());
QPushButton* button6 = new QPushButton(fileInfo6.dir().path());
QVBoxLayout* vbox = new QVBoxLayout;
vbox->addWidget(button1);
vbox->addWidget(button2);
vbox->addWidget(button3);
@ -47,8 +49,6 @@ int main(int argc, char *argv[])
vbox->addWidget(button6);
vbox->addStretch(1);
QGroupBox *groupBox = new QGroupBox("QFileInfo::dir() test");
groupBox->setLayout(vbox);
groupBox->show();
return app.exec();

View File

@ -11,7 +11,7 @@ ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
{
signalMapper = new QSignalMapper(this);
QGridLayout *gridLayout = new QGridLayout;
QGridLayout *gridLayout = new QGridLayout(this);
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, &QPushButton::clicked, signalMapper, &QSignalMapper::map);
@ -23,8 +23,6 @@ ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
connect(signalMapper, &QSignalMapper::mappedString,
//! [1] //! [2]
this, &ButtonWidget::clicked);
setLayout(gridLayout);
}
//! [2]
@ -32,13 +30,12 @@ ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
: QWidget(parent)
{
QGridLayout *gridLayout = new QGridLayout;
QGridLayout *gridLayout = new QGridLayout(this);
for (int i = 0; i < texts.size(); ++i) {
QString text = texts[i];
QPushButton *button = new QPushButton(text);
connect(button, &QPushButton::clicked, [this, text] { clicked(text); });
gridLayout->addWidget(button, i / 3, i % 3);
}
setLayout(gridLayout);
}
//! [3]

View File

@ -28,10 +28,9 @@ FinalWidget::FinalWidget(QWidget *parent, const QString &name,
imageLabel->setMinimumSize(labelSize);
nameLabel = new QLabel(name);
QVBoxLayout *layout = new QVBoxLayout;
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(imageLabel, 1);
layout->addWidget(nameLabel, 0);
setLayout(layout);
}
/*!

View File

@ -61,12 +61,11 @@ ScreenWidget::ScreenWidget(QWidget *parent, QColor initialColor,
connect(colorButton, &QPushButton::clicked, this, &ScreenWidget::setColor);
connect(invertButton, &QPushButton::clicked, this, &ScreenWidget::invertImage);
QGridLayout *gridLayout = new QGridLayout;
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(imageLabel, 0, 0, 1, 2);
gridLayout->addWidget(nameLabel, 1, 0);
gridLayout->addWidget(colorButton, 1, 1);
gridLayout->addWidget(invertButton, 2, 1, 1, 1);
setLayout(gridLayout);
}
/*!

View File

@ -91,11 +91,10 @@ int main(int argc, char **argv)
CombinedTransformation *combinedWidget = new CombinedTransformation;
BasicOperations *basicWidget = new BasicOperations;
QVBoxLayout *layout = new QVBoxLayout;
QVBoxLayout *layout = new QVBoxLayout(&widget);
layout->addWidget(simpleWidget);
layout->addWidget(combinedWidget);
layout->addWidget(basicWidget);
widget.setLayout(layout);
widget.show();
widget.resize(130, 350);

View File

@ -6,11 +6,10 @@ QGraphicsScene scene;
QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);
QGraphicsGridLayout *layout = new QGraphicsGridLayout;
QGraphicsWidget *form = new QGraphicsWidget;
scene.addItem(form);
QGraphicsGridLayout *layout = new QGraphicsGridLayout(form);
layout->addItem(textEdit, 0, 0);
layout->addItem(pushButton, 0, 1);
QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(layout);
scene.addItem(form);
//! [0]

View File

@ -6,11 +6,10 @@ QGraphicsScene scene;
QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
QGraphicsWidget *form = new QGraphicsWidget;
scene.addItem(form);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(form);
layout->addItem(textEdit);
layout->addItem(pushButton);
QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(layout);
scene.addItem(form);
//! [0]

View File

@ -23,9 +23,8 @@ QGroupBox *groupBox = new QGroupBox("Contact Details");
QLabel *numberLabel = new QLabel("Telephone number");
QLineEdit *numberEdit = new QLineEdit;
QFormLayout *layout = new QFormLayout;
QFormLayout *layout = new QFormLayout(groupBox);
layout->addRow(numberLabel, numberEdit);
groupBox->setLayout(layout);
QGraphicsScene scene;
QGraphicsProxyWidget *proxy = scene.addWidget(groupBox);

View File

@ -2,15 +2,16 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
//! [0]
QFormLayout *formLayout = new QFormLayout;
QFormLayout *formLayout = new QFormLayout(this);
formLayout->addRow(tr("&Name:"), nameLineEdit);
formLayout->addRow(tr("&Email:"), emailLineEdit);
formLayout->addRow(tr("&Age:"), ageSpinBox);
setLayout(formLayout);
//! [0]
//! [1]
QGridLayout *gridLayout = new QGridLayout(this);
nameLabel = new QLabel(tr("&Name:"));
nameLabel->setBuddy(nameLineEdit);
@ -20,14 +21,12 @@ emailLabel->setBuddy(emailLineEdit);
ageLabel = new QLabel(tr("&Name:"));
ageLabel->setBuddy(ageSpinBox);
QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(nameLabel, 0, 0);
gridLayout->addWidget(nameLineEdit, 0, 1);
gridLayout->addWidget(emailLabel, 1, 0);
gridLayout->addWidget(emailLineEdit, 1, 1);
gridLayout->addWidget(ageLabel, 2, 0);
gridLayout->addWidget(ageSpinBox, 2, 1);
setLayout(gridLayout);
//! [1]

View File

@ -41,9 +41,8 @@ void MainWindow::createToolBars()
void MainWindow::createDockWidgets()
{
QWidget *dockWidgetContents = new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
QVBoxLayout *layout = new QVBoxLayout(dockWidgetContents);
layout->addWidget(new QPushButton("My Button."));
dockWidgetContents->setLayout(layout);
//! [0]
QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this);

View File

@ -11,13 +11,16 @@
\inmodule QtWidgets
The most common way to use QGraphicsGridLayout is to construct an object
on the heap with no parent, add widgets and layouts by calling addItem(),
and finally assign the layout to a widget by calling
QGraphicsWidget::setLayout(). QGraphicsGridLayout automatically computes
on the heap, passing a parent widget to the constructor, then add widgets
and layouts by calling addItem(). QGraphicsGridLayout automatically computes
the dimensions of the grid as you add items.
\snippet code/src_gui_graphicsview_qgraphicsgridlayout.cpp 0
Alternatively, if you do not pass a parent widget to the layout's constructor,
you will need to call QGraphicsWidget::setLayout() to set this layout as the
top-level layout for that widget, the widget will take ownership of the layout.
The layout takes ownership of the items. In some cases when the layout
item also inherits from QGraphicsItem (such as QGraphicsWidget) there will be a
ambiguity in ownership because the layout item belongs to two ownership hierarchies.

View File

@ -14,12 +14,16 @@
passing Qt::Vertical to QGraphicsLinearLayout's constructor.
The most common way to use QGraphicsLinearLayout is to construct an object
on the heap with no parent, add widgets and layouts by calling addItem(),
and finally assign the layout to a widget by calling
QGraphicsWidget::setLayout().
on the heap, passing a parent widget to the constructor, then add widgets
and layouts by calling addItem().
\snippet code/src_gui_graphicsview_qgraphicslinearlayout.cpp 0
Alternatively, if you do not pass a parent widget to the layout's constructor,
you will need to call QGraphicsWidget::setLayout() to set this layout as the
top-level layout for that widget, the widget will take ownership of
the layout.
You can add widgets, layouts, stretches (addStretch(), insertStretch() or
setStretchFactor()), and spacings (setItemSpacing()) to a linear
layout. The layout takes ownership of the items. In some cases when the layout