Dynamic Layouts Example▲
Dynamic Layouts implements dynamically placed widgets within running applications. The widget placement depends on whether Horizontal or Vertical is chosen.
For more information, visit the Layout Management page.
Dialog Constructor▲
To begin with, the application creates the UI components by calling the following methods:
-
createRotatableGroupBox()
-
createOptionsGroupBox()
-
createButtonBox()
It then adds the UI components to a GridLayout (mainLayout).
Finally, Dialog::rotateWidgets() is called.
createOptionsGroupBox();
createButtonBox();
mainLayout =
new
QGridLayout;
mainLayout-&
gt;addWidget(rotatableGroupBox, 0
, 0
);
mainLayout-&
gt;addWidget(optionsGroupBox, 1
, 0
);
mainLayout-&
gt;addWidget(buttonBox, 2
, 0
);
setLayout(mainLayout);
mainLayout-&
gt;setSizeConstraint(QLayout::
SetMinimumSize);
setWindowTitle(tr("Dynamic Layouts"
));
Creating the Main Widgets▲
The createRotatableGroupBox() method creates a rotatable group box, then adds a series of widgets:
-
QSpinBox
-
QSlider
-
QDial
-
QProgressBar
It goes on to add signals and slots to each widget, and assigns a QGridLayout called rotatableLayout.
void
Dialog::
createRotatableGroupBox()
{
rotatableGroupBox =
new
QGroupBox(tr("Rotatable Widgets"
));
rotatableWidgets.enqueue(new
QSpinBox);
rotatableWidgets.enqueue(new
QSlider);
rotatableWidgets.enqueue(new
QDial);
rotatableWidgets.enqueue(new
QProgressBar);
int
n =
rotatableWidgets.count();
for
(int
i =
0
; i &
lt; n; ++
i) {
connect(rotatableWidgets[i], SIGNAL(valueChanged(int
)),
rotatableWidgets[(i +
1
) %
n], SLOT(setValue(int
)));
}
rotatableLayout =
new
QGridLayout;
rotatableGroupBox-&
gt;setLayout(rotatableLayout);
rotateWidgets();
}
Adding Options▲
createOptionsGroupBox() creates the following widgets:
-
optionsGroupBox
-
buttonsOrientationLabel
-
buttonsOrientationComboBox. The orientation of the ComboBox is either horizontal (default value) or vertical. These two values are added during the startup of the application. It is not possible to leave the option empty.
void
Dialog::
createOptionsGroupBox()
{
optionsGroupBox =
new
QGroupBox(tr("Options"
));
buttonsOrientationLabel =
new
QLabel(tr("Orientation of buttons:"
));
buttonsOrientationComboBox =
new
QComboBox;
buttonsOrientationComboBox-&
gt;addItem(tr("Horizontal"
), Qt::
Horizontal);
buttonsOrientationComboBox-&
gt;addItem(tr("Vertical"
), Qt::
Vertical);
connect(buttonsOrientationComboBox,
QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
currentIndexChanged),
this
,
&
amp;Dialog::
buttonsOrientationChanged);
optionsLayout =
new
QGridLayout;
optionsLayout-&
gt;addWidget(buttonsOrientationLabel, 0
, 0
);
optionsLayout-&
gt;addWidget(buttonsOrientationComboBox, 0
, 1
);
optionsLayout-&
gt;setColumnStretch(2
, 1
);
optionsGroupBox-&
gt;setLayout(optionsLayout);
}
Adding Buttons▲
void
Dialog::
createButtonBox()
{
buttonBox =
new
QDialogButtonBox;
closeButton =
buttonBox-&
gt;addButton(QDialogButtonBox::
Close);
helpButton =
buttonBox-&
gt;addButton(QDialogButtonBox::
Help);
rotateWidgetsButton =
buttonBox-&
gt;addButton(tr("Rotate &Widgets"
),
QDialogButtonBox::
ActionRole);
connect(rotateWidgetsButton, &
amp;QPushButton::
clicked, this
, &
amp;Dialog::
rotateWidgets);
connect(closeButton, &
amp;QPushButton::
clicked, this
, &
amp;Dialog::
close);
connect(helpButton, &
amp;QPushButton::
clicked, this
, &
amp;Dialog::
help);
}
Rotating the Widgets▲
Removes the current widgets and activates the next widget.
void
Dialog::
rotateWidgets()
{
Q_ASSERT(rotatableWidgets.count() %
2
==
0
);
for
(QWidget *
widget : qAsConst(rotatableWidgets))
rotatableLayout-&
gt;removeWidget(widget);
rotatableWidgets.enqueue(rotatableWidgets.dequeue());
const
int
n =
rotatableWidgets.count();
for
(int
i =
0
; i &
lt; n /
2
; ++
i) {
rotatableLayout-&
gt;addWidget(rotatableWidgets[n -
i -
1
], 0
, i);
rotatableLayout-&
gt;addWidget(rotatableWidgets[i], 1
, i);
}
}
Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.