水平布局QHBoxLayout

QHBoxLayout 是一种水平布局控件。

1. QHBoxLayout

属性 说明
layoutLeftMargin 左侧边距。
layoutRightMargin 右侧边距。
layoutTopMargin 顶部边距。
layoutBottomMargin 底部边距。
layoutSpacing 相邻元素间距。

2. QHBoxLayout方法

方法 说明
addWidget 把控件添加到布局管理器。
setLayout 设置布局管理器到……(即设置到 widget 中或其他 layout 中)

3. 代码创建并使用QHBoxLayout管理多个控件

注意要将 QVBoxLayout 的父元素设置到 this 上,且 QVBoxLayout 头文件为 <QLayout>

#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QPushButton* button1=new QPushButton("pushButton1");
    QPushButton* button2=new QPushButton("pushButton2");
    QPushButton* button3=new QPushButton("pushButton3");

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

Widget::~Widget()
{
    delete ui;
}

QHBoxLayout1

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐