Qt布局容器1

本文详细介绍了Qt中的QHBoxLayout布局管理器的使用方法,包括设置元素间隔、边距、拉伸系数和对齐方式。通过实例展示了如何创建和添加彩色背景的QWidget,以及如何调整不同部件的拉伸比例和对齐方式,以实现更灵活的界面布局。同时,还探讨了通过添加子布局来实现更复杂的布局结构。

Qt提供了以下几种布局容器

QHBoxLayout:横向布局 , 继承自QBoxLayout,QBoxLayout继承自QLayout
QVBoxLayout:纵向布局 , 继承自QBoxLayout
QGridLayout:表格布局 , 继承自QLayout
QFormLayout:表单布局 ,继承自QLayout

学习使用QHBoxLayout布局容器

setSpacing设置元素间隔,参数整型值
setMargin设置边距,是个整型值
addStretch弹簧主要作用是占位,参数整形值,占位系数

QHBoxLayout成员方法:

void addWidget(QWidget *, int stretch = 0, Qt::Alignment alignment = Qt::Alignment());
三个参数:

参数1:窗口指针
参数2:窗口size拉伸系数(具有更高拉伸系数的部件会长得更多)
参数3:对齐方式,上下左右居中等.......

The horizontal flags are:
  Constant           Value             Description
Qt::AlignLeft      0x0001              靠左边排列
Qt::AlignRight     0x0002              靠右边排列
Qt::AlignHCenter   0x0004              水平居中排列
Qt::AlignJustify   0x0008              靠左右两边排列

The vertical flags are:
ConstantValueDescription
Qt::AlignTop       0x0020              靠顶部排列
Qt::AlignBottom    0x0040              靠下边排列
Qt::AlignVCenter   0x0080              垂直居中排列
Qt::AlignBaseline  0x0100              与基线对齐排列

You can use only one of the horizontal flags at a time. 
There is one two-dimensional flag:
   Constant               Value                                Description
Qt::AlignCenter   AlignVCenter | AlignHCenter                   居中排列

创建12个彩色背景的QWidget,添加进QHBoxLayout布局容器里,便于测试


参数2和参数3使用了默认值
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QDebug>



Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
   
   
    setMinimumSize(600,400);
    QHBoxLayout* h = new QHBoxLayout(
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值