【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout


  • 博客主页:Duck Bro 博客主页
  • 系列专栏:Qt 专栏
  • 关注博主,后期持续更新系列文章
  • 如果有错误感谢请大家批评指出,及时修改
  • 感谢大家点赞👍收藏⭐评论✍

Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout

文章编号:Qt 学习笔记 / 42

文章目录

  • [Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout](#Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout)
    • [一、 QHBoxLayout介绍](#一、 QHBoxLayout介绍)
      • [1. 简介](#1. 简介)
      • [2. 核心属性](#2. 核心属性)
    • [二、 QHBoxLayout使用](#二、 QHBoxLayout使用)
      • [1. 使用代码创建水平布局管理控件](#1. 使用代码创建水平布局管理控件)
      • [2. 布局嵌套(垂直布局嵌套水平布局)](#2. 布局嵌套(垂直布局嵌套水平布局))
      • [3. 图形化实现嵌套布局](#3. 图形化实现嵌套布局)

一、 QHBoxLayout介绍

1. 简介

QHBoxLayout(水平布局)是Qt中的一种布局管理器,用于在水平方向上排列子控件。它是QBoxLayout的一个子类。

使用QHBoxLayout可以将子控件按照从左到右的顺序排列,子控件之间的间距可以通过设置布局的spacing属性来调整。

2. 核心属性

属性 说明
layoutLeftMargin 左侧边距
layoutRightMargin 右侧边距
layoutTopMargin 上⽅边距
layoutBottomMargin 下⽅边距
layoutSpacing 相邻元素之间的间距

二、 QHBoxLayout使用

1. 使用代码创建水平布局管理控件

  1. 编辑程序,创建三个按钮和一个水平布局,代码如下
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QHBoxLayout>
#include <QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //创建水平布局
    QHBoxLayout * layout = new QHBoxLayout();
    //创建三个按钮
    QPushButton *button1 =new QPushButton("按钮1");
    QPushButton *button2 =new QPushButton("按钮2");
    QPushButton *button3 =new QPushButton("按钮3");
    //将按钮设置到水平布局中
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    //设置layout到widget中
    this->setLayout(layout);
}

Widget::~Widget()
{
    delete ui;
}
  1. 运行代码,查看结果,如下图所示

2. 布局嵌套(垂直布局嵌套水平布局)

  1. 使用代码编写嵌套布局
cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建顶层 layout
    QVBoxLayout* layoutParent = new QVBoxLayout();
    this->setLayout(layoutParent);
    
    // 添加两个按钮进去
    QPushButton* btn1 = new QPushButton("按钮1");
    QPushButton* btn2 = new QPushButton("按钮2");
    layoutParent->addWidget(btn1);
    layoutParent->addWidget(btn2);
    
    // 创建⼦ layout
    QHBoxLayout* layoutChild = new QHBoxLayout();
    
    // 添加两个按钮进去
    QPushButton* btn3 = new QPushButton("按钮3");
    QPushButton* btn4 = new QPushButton("按钮4");
    layoutChild->addWidget(btn3);
    layoutChild->addWidget(btn4);
    
    // 把这个⼦layout 添加到父layout 中
    layoutParent->addLayout(layoutChild);
}

Widget::~Widget()
{
    delete ui;
}
  1. 运行结果,查看结果

3. 图形化实现嵌套布局

  1. 使用图形化界面创建嵌套布局,在垂直布局中嵌套水平布局

  2. 运行代码,查看结果


相关推荐
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
西岸行者4 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
starlaky4 天前
Django入门笔记
笔记·django
勇气要爆发4 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js