Basic Layouts Example▲
Basic Layouts shows how to use the standard layout managers that are available in Qt: QBoxLayout, QGridLayout, and QFormLayout.
The QBoxLayout class lines up widgets horizontally or vertically. QHBoxLayout and QVBoxLayout are convenience subclasses of QBoxLayout. QGridLayout lays out widgets in cells by dividing the available space into rows and columns. QFormLayout, on the other hand, sets its children in a two-column form with labels in the left column and input fields in the right column.
For more information, visit the Layout Management page.
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.
Dialog Class Definition▲
class
Dialog : public
QDialog
{
Q_OBJECT
public
:
Dialog();
private
:
void
createMenu();
void
createHorizontalGroupBox();
void
createGridGroupBox();
void
createFormGroupBox();
enum
{
NumGridRows =
3
, NumButtons =
4
}
;
QMenuBar *
menuBar;
QGroupBox *
horizontalGroupBox;
QGroupBox *
gridGroupBox;
QGroupBox *
formGroupBox;
QTextEdit *
smallEditor;
QTextEdit *
bigEditor;
QLabel *
labels[NumGridRows];
QLineEdit *
lineEdits[NumGridRows];
QPushButton *
buttons[NumButtons];
QDialogButtonBox *
buttonBox;
QMenu *
fileMenu;
QAction *
exitAction;
}
;
The Dialog class inherits QDialog. It is a custom widget that displays its child widgets using the geometry managers: QHBoxLayout, QVBoxLayout, QGridLayout, and QFormLayout.
There are four private functions to simplify the class constructor: the createMenu(), createHorizontalGroupBox(), createGridGroupBox(), and createFormGroupBox() functions create several widgets that the example uses to demonstrate how the layout affects their appearances.
Dialog Class Implementation▲
Dialog::
Dialog()
{
createMenu();
createHorizontalGroupBox();
createGridGroupBox();
createFormGroupBox();
In the constructor, we first use the createMenu() function to create and populate a menu bar and the createHorizontalGroupBox() function to create a group box containing four buttons with a horizontal layout. Next, we use the createGridGroupBox() function to create a group box containing several line edits and a small text editor which are displayed in a grid layout. Finally, we use the createFormGroupBox() function to create a group box with three labels and three input fields: a line edit, a combo box, and a spin box.
bigEditor =
new
QTextEdit;
bigEditor-&
gt;setPlainText(tr("This widget takes up all the remaining space "
"in the top-level layout."
));
buttonBox =
new
QDialogButtonBox(QDialogButtonBox::
Ok
|
QDialogButtonBox::
Cancel);
connect(buttonBox, &
amp;QDialogButtonBox::
accepted, this
, &
amp;QDialog::
accept);
connect(buttonBox, &
amp;QDialogButtonBox::
rejected, this
, &
amp;QDialog::
reject);
We also create a big text editor and a dialog button box. The QDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the current widget style. The preferred buttons can be specified as arguments to the constructor, using the QDialogButtonBox::StandardButtons enum.
Note that we don't have to specify a parent for the widgets when we create them. The reason is that all the widgets we create here will be added to a layout, and when we add a widget to a layout, it is automatically reparented to the widget the layout is installed on.
QVBoxLayout