首页 > 其他分享 >Qt之水平布局(QHBoxLayout)

Qt之水平布局(QHBoxLayout)

时间:2023-05-19 11:57:46浏览次数:35  
标签:QHBoxLayout layout Qt 布局 QPushButton addWidget new

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);

 

标签:QHBoxLayout,layout,Qt,布局,QPushButton,addWidget,new
From: https://www.cnblogs.com/QingYiShouJiuRen/p/17414641.html

相关文章

  • dbus -qt
    xml   D-Bus等价类型   Qt数据类型as   Arrayof[string]   QList<QString>a{sv}   dictofstring   QVariantMapa{sa{sv}}   dictofobjectpath   QMap<QString,QVariantMap>a{oa{sa{sv}}}   dictofobjectpath   QMap<QDBusObje......
  • 写一个左中右布局占满屏幕,其中左右两块是苦丁宽200px,中间自适应宽,要求先加载中间块。
    <htmllang="en"><body><divclass="container"><divclass="main">中间</div><divclass="left">左边</div><divclass=......
  • Build QtAV
    gitaddr:https://github.com/wang-bin/QtAV UninstallQtAVSDKbeforebuildingtoavoidheaderfilesconfliction.Runsdk_uninstall.bat/shunderyourbuilddirShadowbuildisrecommended(requiredformacbuild).0.PrerequisitesGetQtAVsourcecodeu......
  • 在Linux的Ubuntu系统下安装QT及相关环境配置
    qt下载地址:http://download.qt.io/archive/qt/?tdsourcetag=s_pcqq_aiomsg 安装QT    从官网下载QT的安装包,在本地安装。本次使用的是Ubuntu18.06以及QT5.14.2,下文皆使用此版本作为示例。    首先安装QT,具体操作如下: 1、使用cd命令:cd/home/(usr)/download,切......
  • 料箱输送线程序,带目的地跟踪,提供设备布局图和电气图纸以及博途程序。
    料箱输送线程序,带目的地跟踪,提供设备布局图和电气图纸以及博途程序。程序语言较多使用了STL,程序仅供学习参考。硬件配置:PLC:1515-2PNHMI:TP700Confort主要设备有:英特诺直流辊筒电机,控制卡MultiControl;条码阅读器SickCLV620;和MiniLoad堆垛机通过Anybus模块通讯;称重模块品牌碧彩B......
  • 传动带料箱输送线程序,带目的地跟踪,提供设备布局图和电气图纸以及博途程序。
    传动带料箱输送线程序,带目的地跟踪,提供设备布局图和电气图纸以及博途程序。程序语言较多使用了STL,程序仅供学习参考。硬件配置:PLC:1515-2PNHMI:TP700Confort主要设备有:英特诺直流辊筒电机,控制卡MultiControl;条码阅读器SickCLV620;和MiniLoad堆垛机通过Anybus模块通讯;称重模块品......
  • Docker 安装 MQTT(EMQX)
    Docker安装MQTT(EMQX)[root@localhost~]#dockerpullemqx:4.4.164.4.16:Pullingfromlibrary/emqx3f9582a2cbe7:Pullcomplete396ee3d6a271:Pullcompletef79aa88ad721:Pullcomplete8943a0bcb1f0:Pullcomplete66a2b18c61a7:PullcompleteDigest:sh......
  • 如何使用Go与MQTT进行通信
    简介本文介绍了如何使用Go编程语言与MQTT(MessageQueuingTelemetryTransport)进行通信。MQTT是一种轻量级的消息传输协议,广泛应用于物联网和实时通信场景。通过本文的指导,您将学习如何使用Go语言创建MQTT客户端,进行消息的发布和订阅。准备工作在开始之前,请确保您已完......
  • QT6 环境搭建和简单例子
    环境搭建$python-VPython3.9.16QT6需要Python3.7+以上版本。$python-mvenvenvqt6在这个命令中,参数-m表示执行模块的方式。它告诉Python解释器以模块的方式执行后面提供的参数。venv是一个Python模块,用于创建和管理虚拟环境。当你运行python-mv......
  • QT5 环境搭建和简单例子
    环境搭建$python-VPython3.9.16QT5需要Python3.7以上版本。$python-mvenvenvqt5在这个命令中,参数-m表示执行模块的方式。它告诉Python解释器以模块的方式执行后面提供的参数。venv是一个Python模块,用于创建和管理虚拟环境。当你运行python-mve......