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 vertically
  • QHBoxLayout: This lays out its view children horizontally
  • QVBoxLayout: This lays out its view children vertically
  • QFormLayout: 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 form
  • QGridLayout: This lays out widgets in a grid
  • QStackedLayout: 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("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);

window->setLayout(layout);
window->show();

Layouts work in conjunction with a widget's sizePolicy and sizeHint properties. These properties provide information to the layout and layout manager about how the widgets should be laid out. The sizePolicy property is an instance of the class QSizePolicy and controls layout preferences to choose between layouts in both the horizontal and vertical directions, offering the following choices in each direction:

  • QSizePolicy::Fixed: Here, the widget's sizeHint property is the only size the widget should be
  • QSizePolicy::Minimum: Here, the widget's size is the minimum it can be, and there's no advantage to it being larger
  • QSizePolicy::Maximum: Here, the widget's size is the maximum size it can be; it can be smaller, but should be no larger
  • QSizePolicy::Preferred: Here, the widget's sizeHint property is respected if it can be
  • QSizePolicy::Expanding: This is used to indicate that the sizeHint property is a recommendation but that more space can be used if it's available.
  • QSizePolicy::MinimumExpanding: This is used to indicate that sizeHint is minimal and sufficient, but that more space can be used if it's available.

Widgets in Qt Widgets have size policies that make sense for the general UI constraints of the target platform, and you typically won't need to change the policy with QSizePolicy::setVerticalPolicy or QSizePolicy::setHorizontalPolicy.

Tip

Use the layout classes and their defaults as much as you can in your application to ensure cross-platform compatibility and proper layout on screens of different sizes. If you're worrying about individual pixel placement for your widgets, you're likely doing something wrong and will end up with a user interface that doesn't look like what you expect on at least some systems some of the time.

For more information about managing widget layouts with the layout classes, see the Qt documentation at http://qt-project.org/doc/qt-5/layout.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset