Qt中的水平|垂直布局管理器(QBoxLayout、QHboxLayout和QVBoxLayout)

本文主要是介绍Qt中的水平|垂直布局管理器(QBoxLayout、QHboxLayout和QVBoxLayout),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

QHBoxLayout 类水平排列小部件。

下面介绍水平布局管理器的使用方法:

效果

 

源码:

    QHBoxLayout* hLayout = new QHBoxLayout();QPushButton* pbtn1 = new QPushButton("button 1");QPushButton* pbtn2 = new QPushButton("button 2");QPushButton* pbtn3 = new QPushButton("button 3");QPushButton* pbtn4 = new QPushButton("button 4");QPushButton* pbtn5 = new QPushButton("button 5");hLayout->addWidget(pbtn1);hLayout->addWidget(pbtn2);hLayout->addWidget(pbtn3);hLayout->addWidget(pbtn4);hLayout->addWidget(pbtn5);setWindowTitle(QString::fromLocal8Bit("布局管理器-QHboxLayout"));setLayout(hLayout);qDebug() << "this: " << this;qDebug() << pbtn1->text() + " Parent:" << pbtn1->parent();qDebug() << pbtn2->text() + " Parent:" << pbtn2->parent();qDebug() << pbtn3->text() + " Parent:" << pbtn3->parent();qDebug() << pbtn4->text() + " Parent:" << pbtn4->parent();qDebug() << pbtn5->text() + " Parent:" << pbtn5->parent();

 结果是 同一个QHBoxLayout管理的组件有相同的父组件

接口分析

设置外边距的函数:

  • void setMargin(int)
  • void setContentsMargins(int left, int top, int right, int bottom)
  • void setContentsMargins(const QMargins &margins)
hLayout->setContentsMargins(20, 20, 20, 20);

运行效果:

 设置间距:

此属性保存布局内小部件之间的间距

  • void setSpacing(int)
hLayout->setSpacing(0);

运行效果图:

设置组件伸缩比例系数:

  • void setStretch(int index, int stretch)
  • bool setStretchFactor(QWidget *widget, int stretch)
  • bool setStretchFactor(QLayout *layout, int stretch)
    hLayout->setStretch(0, 1);hLayout->setStretch(1, 2);hLayout->setStretch(2, 3);

运行效果图:

 添加一个伸缩空间:

将大小为 size 的不可拉伸空间(QSpacerItem)添加到此框布局的末尾。 QBoxLayout 提供默认的边距和间距。 此功能增加了额外的空间。

  • void addSpacing(int size)
    hLayout->addStretch(0);hLayout->addWidget(pbtn1);hLayout->addWidget(pbtn2);hLayout->addWidget(pbtn3);hLayout->addWidget(pbtn4);hLayout->addWidget(pbtn5);hLayout->addStretch(0);

运行效果图:


QVBoxLayout 类垂直排列小部件。

 下面介绍QVBoxLayout使用方法:

效果图:

源码:

    QVBoxLayout* vLayout = new QVBoxLayout();QPushButton* pbtn1 = new QPushButton("button 1");QPushButton* pbtn2 = new QPushButton("button 2");QPushButton* pbtn3 = new QPushButton("button 3");QPushButton* pbtn4 = new QPushButton("button 4");QPushButton* pbtn5 = new QPushButton("button 5");vLayout->addWidget(pbtn1);vLayout->addWidget(pbtn2);vLayout->addWidget(pbtn3);vLayout->addWidget(pbtn4);vLayout->addWidget(pbtn5);setWindowTitle(QString::fromLocal8Bit("布局管理器-QHboxLayout"));setLayout(vLayout);qDebug() << "this: " << this;qDebug() << pbtn1->text() + " Parent:" << pbtn1->parent();qDebug() << pbtn2->text() + " Parent:" << pbtn2->parent();qDebug() << pbtn3->text() + " Parent:" << pbtn3->parent();qDebug() << pbtn4->text() + " Parent:" << pbtn4->parent();qDebug() << pbtn5->text() + " Parent:" << pbtn5->parent();

 

 结果是 同一个QVBoxLayout管理的组件有相同的父组件。 


QHBoxLayout和QVBoxLayout嵌套使用:

运行效果图:

源码:

    QVBoxLayout* vLayout = new QVBoxLayout();QHBoxLayout* hLayout1 = new QHBoxLayout();QHBoxLayout* hLayout2 = new QHBoxLayout();QPushButton* pbtn1 = new QPushButton("button 1");QPushButton* pbtn2 = new QPushButton("button 2");QPushButton* pbtn3 = new QPushButton("button 3");QPushButton* pbtn4 = new QPushButton("button 4");QPushButton* pbtn5 = new QPushButton("button 5");QPushButton* pbtn6 = new QPushButton("button 6");QPushButton* pbtn7 = new QPushButton("button 7");QPushButton* pbtn8 = new QPushButton("button 8");QPushButton* pbtn9 = new QPushButton("button 9");QPushButton* pbtn0 = new QPushButton("button 0");hLayout1->addWidget(pbtn1);hLayout1->addWidget(pbtn2);hLayout1->addWidget(pbtn3);hLayout1->addWidget(pbtn4);hLayout1->addWidget(pbtn5);hLayout2->addWidget(pbtn6);hLayout2->addWidget(pbtn7);hLayout2->addWidget(pbtn8);hLayout2->addWidget(pbtn9);hLayout2->addWidget(pbtn0);vLayout->addLayout(hLayout1);vLayout->addLayout(hLayout2);setLayout(vLayout);setWindowTitle(QString::fromLocal8Bit("布局管理器-QBoxLayout嵌套使用"));    

 

这篇关于Qt中的水平|垂直布局管理器(QBoxLayout、QHboxLayout和QVBoxLayout)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/166118

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

Python实现剪贴板历史管理器

《Python实现剪贴板历史管理器》在日常工作和编程中,剪贴板是我们使用最频繁的功能之一,本文将介绍如何使用Python和PyQt5开发一个功能强大的剪贴板历史管理器,感兴趣的可以了解下... 目录一、概述:为什么需要剪贴板历史管理二、功能特性全解析2.1 核心功能2.2 增强功能三、效果展示3.1 主界面