Managing the widget layout with layouts
Qt Widgets includes a robust layout system to control the presentation of widgets on the display. In Qt Creator Designer, you can pick from the following layouts:
QBoxLayout
: This lays out its view children horizontally or verticallyQHBoxLayout
: This lays out its view children horizontallyQVBoxLayout
: This lays out its view children verticallyQFormLayout
: This lays out pairs of widgets (such as labels and textboxes) side by side and then tiles those pairs vertically, giving the appearance of a formQGridLayout
: This lays out widgets in a gridQStackedLayout
: This shows only a single widget at a time
Using one of these layouts is easy: simply choose the appropriate layout in Qt Creator Designer and drag it to the widget or window you're building. If you're constructing a hierarchy of widgets in the code, you add the widgets to the layout and set the parent widget's layout, like this:
QWidget *window = new QWidget(); QPushButton *button1 = new QPushButton...