Qt之水平布局(QHBoxLayout)

青衣守旧人 / 2023-05-19 / 原文

QHBoxLayout是Qt框架中的一个布局管理器类,用于水平排列子部件。它是QLayout类的子类,用于在水平方向上自动调整和布局子部件的位置和大小。

以下是QHBoxLayout的一些特点和用法:

1. 水平布局:QHBoxLayout将子部件按照水平方向从左到右进行布局。它可以自动调整子部件的位置和大小,使它们适应布局容器的大小变化。

2. 添加子部件:通过调用QHBoxLayout的addWidget()函数,可以向布局中添加子部件。可以添加各种Qt部件,如按钮、标签、文本框等。

3. 弹性空间:QHBoxLayout支持弹性空间的概念。通过在子部件之间添加弹性空间(addStretch()),可以实现在布局中分配额外的空间或者平均分配剩余空间。

4. 对齐方式:可以使用setAlignment()函数设置子部件在布局中的对齐方式。可以指定水平和垂直方向上的对齐方式,如左对齐、右对齐、居中等。

5. 嵌套布局:QHBoxLayout可以与其他布局管理器一起使用,以实现更复杂的布局。例如,可以将QHBoxLayout嵌套在QVBoxLayout中,以实现水平和垂直方向上的布局。

一、QHBoxLayout的使用

#include "main_window.h"

#include <QHBoxLayout>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    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;
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(button5);
    layout->setAlignment(Qt::AlignTop);

    this->setLayout(layout);
}

 二、常用函数

(1).void QBoxLayout::setSpacing(int spacing)

用来设置控件之间的间距,使用setSpacing(15)将间距设置为15

layout->setSpacing(15);

(2).void QLayout::setContentsMargins(int left, int top, int right, int bottom)

设置外边距,将左、上、右、下的外边距设置为不同的值。

layout->setContentsMargins(30, 5, 5, 5);

 setMargin(int)可以设置左、上、右、下的外边距,设置之后,他们的外边距是相同的。

(3).void QBoxLayout::addStretch(int stretch = 0) 添加了一个伸缩空间,默认值为0

居右添加伸缩

this->setFixedWidth(600);

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;
layout->addStretch();
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
layout->setAlignment(Qt::AlignTop);

this->setLayout(layout);

居左添加伸缩

this->setFixedWidth(600);

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;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
layout->addStretch();
layout->setAlignment(Qt::AlignTop);

this->setLayout(layout);

居中添加伸缩

this->setFixedWidth(600);

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;
layout->addStretch(); /* 第一个控件之前添加伸缩 */
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
layout->addStretch(); /* 最后一个控件之后添加伸缩 */
layout->setAlignment(Qt::AlignTop);

this->setLayout(layout);

如果在每一个控件之间都添加伸缩,那么所有的控件之间的间距都会相同。

this->setFixedWidth(600);

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;
layout->addStretch();
layout->addWidget(button1);
layout->addStretch();
layout->addWidget(button2);
layout->addStretch();
layout->addWidget(button3);
layout->addStretch();
layout->addWidget(button4);
layout->addStretch();
layout->addWidget(button5);
layout->addStretch();
layout->setAlignment(Qt::AlignTop);

this->setLayout(layout);

 (4).void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

通常我们使用addWidget添加的控件都是默认垂直方向居中对齐的。其中有控件大小相同的时候就会看得不是很明显,一旦有不同于其他控件大小的控件出现,那么就会很明显的看出来,如下所示:

如果我们需要将其中的某些控件居上、居下显示,那么可以使用对齐方式Qt::Alignment。

 

this->setFixedWidth(600);

QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
button3->setFixedHeight(40);
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");

QHBoxLayout *layout = new QHBoxLayout;
 /* 水平居左 垂直居上 */
layout->addWidget(button1, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(button2, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(button3);
 /* 水平居左 垂直居下 */
layout->addWidget(button4, 0, Qt::AlignLeft | Qt::AlignBottom);
layout->addWidget(button5, 0, Qt::AlignLeft | Qt::AlignBottom);
layout->setSpacing(15);

this->setLayout(layout);

(5).void QBoxLayout::setDirection(QBoxLayout::Direction direction)

设置布局方向,可以设置从左到右、从右到左、从上到下、从下到上等.

layout->setDirection(QBoxLayout::RightToLeft);

layout->setDirection(QBoxLayout::TopToBottom);