Skip to content

Commit

Permalink
Doc: Complete Dynamic Layouts Example
Browse files Browse the repository at this point in the history
- add doc to code
- replace old signal/slot syntax
- more up-to-date screenshot

Task-number: QTBUG-60635
Change-Id: Iec3d2c3d0d8b9c07ccd4446d74d5eca2d88e7e08
Reviewed-by: Paul Wicking <[email protected]>
Reviewed-by: Topi Reiniö <[email protected]>
  • Loading branch information
vertrni committed Mar 14, 2018
1 parent 00304ea commit 421cfd9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 29 deletions.
Binary file added doc/src/images/dynamiclayouts-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions examples/widgets/doc/src/dynamiclayouts.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,82 @@
applications. The widget placement depends on whether \c Horizontal or \c
Vertical is chosen.

\borderedimage dynamiclayouts-example.png
For more information, visit the \l{Layout Management} page.

\section1 Dialog Constructor

To begin with, the application creates the UI components by calling the
following methods:

\list
\li createRotatableGroupBox()
\li createOptionsGroupBox()
\li createButtonBox()
\endlist

It then adds the UI components to a GridLayout (\c mainLayout).

Finally, \c Dialog::rotateWidgets() is called.

\quotefromfile layouts/dynamiclayouts/dialog.cpp
\skipuntil createRotatableGroupBox
\printuntil setWindowTitle

\section1 Creating the Main Widgets

The \c createRotatableGroupBox() method creates a rotatable group box,
then adds a series of widgets:

\list
\li QSpinBox
\li QSlider
\li QDial
\li QProgressBar
\endlist

It goes on to add signals and slots to each widget, and assigns
a QGridLayout called \a rotatableLayout.

\skipto Dialog::createRotatableGroupBox
\printuntil /^\}/

\section1 Adding Options

\c createOptionsGroupBox() creates the following widgets:
\list
\li \c optionsGroupBox
\li \c buttonsOrientationLabel
\li \c buttonsOrientationComboBox. The orientation of the ComboBox is either
\c horizontal (default value) or \c vertical. These two values
are added during the startup of the application. It is not possible
to leave the option empty.
\endlist

\skipto Dialog::createOptionsGroupBox()
\printuntil /^\}/

\section1 Adding Buttons

createButtonBox() constructs a QDialogButtonBox called \c buttonBox
to which are added a \c closeButton, a \c helpButton and a
\c rotateWidgetsButton.
It then assigns a signal and a slot to each button in \c buttonBox.

\skipto Dialog::createButtonBox()
\printuntil /^\}/


\section1 Rotating the Widgets

Removes the current widgets and activates the next widget.

\quotefromfile layouts/dynamiclayouts/dialog.cpp
\skipto Dialog::rotateWidgets()
\printuntil rotatableLayout->addWidget(rotatableWidgets[i]
\printuntil }
\printuntil }

\include examples-run.qdocinc
*/

54 changes: 29 additions & 25 deletions examples/widgets/layouts/dynamiclayouts/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
createRotableGroupBox();
createRotatableGroupBox();
createOptionsGroupBox();
createButtonBox();

mainLayout = new QGridLayout;
mainLayout->addWidget(rotableGroupBox, 0, 0);
mainLayout->addWidget(rotatableGroupBox, 0, 0);
mainLayout->addWidget(optionsGroupBox, 1, 0);
mainLayout->addWidget(buttonBox, 2, 0);
setLayout(mainLayout);
Expand Down Expand Up @@ -102,17 +102,17 @@ void Dialog::buttonsOrientationChanged(int index)

void Dialog::rotateWidgets()
{
Q_ASSERT(rotableWidgets.count() % 2 == 0);
Q_ASSERT(rotatableWidgets.count() % 2 == 0);

foreach (QWidget *widget, rotableWidgets)
rotableLayout->removeWidget(widget);
foreach (QWidget *widget, rotatableWidgets)
rotatableLayout->removeWidget(widget);

rotableWidgets.enqueue(rotableWidgets.dequeue());
rotatableWidgets.enqueue(rotatableWidgets.dequeue());

const int n = rotableWidgets.count();
const int n = rotatableWidgets.count();
for (int i = 0; i < n / 2; ++i) {
rotableLayout->addWidget(rotableWidgets[n - i - 1], 0, i);
rotableLayout->addWidget(rotableWidgets[i], 1, i);
rotatableLayout->addWidget(rotatableWidgets[n - i - 1], 0, i);
rotatableLayout->addWidget(rotatableWidgets[i], 1, i);
}
}

Expand All @@ -123,23 +123,23 @@ void Dialog::help()
"dynamically."));
}

void Dialog::createRotableGroupBox()
void Dialog::createRotatableGroupBox()
{
rotableGroupBox = new QGroupBox(tr("Rotable Widgets"));
rotatableGroupBox = new QGroupBox(tr("Rotatable Widgets"));

rotableWidgets.enqueue(new QSpinBox);
rotableWidgets.enqueue(new QSlider);
rotableWidgets.enqueue(new QDial);
rotableWidgets.enqueue(new QProgressBar);
rotatableWidgets.enqueue(new QSpinBox);
rotatableWidgets.enqueue(new QSlider);
rotatableWidgets.enqueue(new QDial);
rotatableWidgets.enqueue(new QProgressBar);

int n = rotableWidgets.count();
int n = rotatableWidgets.count();
for (int i = 0; i < n; ++i) {
connect(rotableWidgets[i], SIGNAL(valueChanged(int)),
rotableWidgets[(i + 1) % n], SLOT(setValue(int)));
connect(rotatableWidgets[i], SIGNAL(valueChanged(int)),
rotatableWidgets[(i + 1) % n], SLOT(setValue(int)));
}

rotableLayout = new QGridLayout;
rotableGroupBox->setLayout(rotableLayout);
rotatableLayout = new QGridLayout;
rotatableGroupBox->setLayout(rotatableLayout);

rotateWidgets();
}
Expand All @@ -154,8 +154,10 @@ void Dialog::createOptionsGroupBox()
buttonsOrientationComboBox->addItem(tr("Horizontal"), Qt::Horizontal);
buttonsOrientationComboBox->addItem(tr("Vertical"), Qt::Vertical);

connect(buttonsOrientationComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(buttonsOrientationChanged(int)));
connect(buttonsOrientationComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&Dialog::buttonsOrientationChanged);

optionsLayout = new QGridLayout;
optionsLayout->addWidget(buttonsOrientationLabel, 0, 0);
Expand All @@ -173,7 +175,9 @@ void Dialog::createButtonBox()
rotateWidgetsButton = buttonBox->addButton(tr("Rotate &Widgets"),
QDialogButtonBox::ActionRole);

connect(rotateWidgetsButton, SIGNAL(clicked()), this, SLOT(rotateWidgets()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(helpButton, SIGNAL(clicked()), this, SLOT(help()));
connect(rotateWidgetsButton, &QPushButton::clicked, this, &Dialog::rotateWidgets);
connect(closeButton, &QPushButton::clicked, this, &Dialog::close);
connect(helpButton, &QPushButton::clicked, this, &Dialog::help);
}


8 changes: 4 additions & 4 deletions examples/widgets/layouts/dynamiclayouts/dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ private slots:
void help();

private:
void createRotableGroupBox();
void createRotatableGroupBox();
void createOptionsGroupBox();
void createButtonBox();

QGroupBox *rotableGroupBox;
QQueue<QWidget *> rotableWidgets;
QGroupBox *rotatableGroupBox;
QQueue<QWidget *> rotatableWidgets;

QGroupBox *optionsGroupBox;
QLabel *buttonsOrientationLabel;
Expand All @@ -93,7 +93,7 @@ private slots:
QPushButton *rotateWidgetsButton;

QGridLayout *mainLayout;
QGridLayout *rotableLayout;
QGridLayout *rotatableLayout;
QGridLayout *optionsLayout;
};

Expand Down

0 comments on commit 421cfd9

Please sign in to comment.