QHBoxLayout Class

The QHBoxLayout class lines up widgets horizontally. More...

Header: #include <QHBoxLayout>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
Inherits: QBoxLayout

Public Functions

QHBoxLayout()
QHBoxLayout(QWidget *parent)
virtual ~QHBoxLayout()

Detailed Description

This class is used to construct horizontal box layout objects. See QBoxLayout for details.

The simplest use of the class is like this:

     QWidget *window = new QWidget;
     QPushButton *button1 = new QPushButton("One");
     QPushButton *button2 = new QPushButton("Two");
     QPushButton *button3 = new QPushButton("Three");
     QPushButton *button4 = new QPushButton("Four");
     QPushButton *button5 = new QPushButton("Five");

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

     window->show();

First, we create the widgets we want to add to the layout. Then, we create the QHBoxLayout object, setting window as parent by passing it in the constructor; next we add the widgets to the layout. window will be the parent of the widgets that are added to the layout.

If you don't pass a parent window to the constructor, you can at a later point use QWidget::setLayout() to install the QHBoxLayout object onto window. At that point, the widgets in the layout are reparented to have window as their parent.

Horizontal box layout with five child widgets

See also QVBoxLayout, QGridLayout, QStackedLayout, Layout Management, and Basic Layouts Example.

Member Function Documentation

QHBoxLayout::QHBoxLayout()

Constructs a new horizontal box. You must add it to another layout.

[explicit] QHBoxLayout::QHBoxLayout(QWidget *parent)

Constructs a new top-level horizontal box with parent parent.

The layout is set directly as the top-level layout for parent. There can be only one top-level layout for a widget. It is returned by QWidget::layout().

See also QWidget::setLayout().

[virtual noexcept] QHBoxLayout::~QHBoxLayout()

Destroys this box layout.

The layout's widgets aren't destroyed.