Qt应用开发(Quick篇)——布局类与布局模块

news2025/6/10 4:33:43

一、前言

       实际 应用中,布局是常用的功能,布局最直观的就是提供空间使用率,改善空间的流动和模块之间的重叠,让界面更加的美观

二、布局类Layout

2.1 介绍

         将Layout类型的对象附加到布局的子元素上,提供有关该项的特定于布局的信息,附加对象的属性也会影响布局安排项目的方式。

        提供最小宽度minimumWidth最大宽度maximumWidth首选宽度preferredWidth等影响元素的宽度,填充宽度fillWidth填充高度fillHeight属性如果它们为false,则项目的大小将固定为首选大小,否则,当布局调整大小时,它将在最小最大之间增长或缩小。

 ColumnLayout{
     spacing: 2

     Rectangle {
         Layout.alignment: Qt.AlignCenter
         color: "red"
         Layout.preferredWidth: 40
         Layout.preferredHeight: 40
     }

     Rectangle {
         Layout.alignment: Qt.AlignRight
         color: "green"
         Layout.preferredWidth: 40
         Layout.preferredHeight: 70
     }

     Rectangle {
         Layout.alignment: Qt.AlignBottom
         Layout.fillHeight: true
         color: "blue"
         Layout.preferredWidth: 70
         Layout.preferredHeight: 40
     }
 }

        注意:不要绑定到布局中项目的x、y、width或heigh属性,因为这将与布局的目标相冲突,并且还可能导致绑定循环。布局引擎使用宽度和高度属性来存储从最小/首选/最大附加属性计算出来的项目的当前大小,并且可以在每次布局项目时重写。

2.2 依附属性

Layout.alignment : Qt.Alignment

        此属性表示元素在其占用的单元格内的对齐方式,默认为左对齐+纵向居中,也就是Qt.AlignVCenter | Qt.AlignLeft,如果只指定了水平标志,默认的垂直标志将是Qt.AlignVCenter,如果只指定了垂直标志,默认的水平标志将是Qt.AlignLeft。

        有效的对齐方式有:

  • Qt::AlignLeft                水平左对齐
  • Qt::AlignHCenter         水平居中
  • Qt::AlignRight              水平右对齐
  • Qt::AlignTop                 垂直顶部对齐
  • Qt::AlignVCenter          垂直居中
  • Qt::AlignBottom            垂直底部对齐
  • Qt::AlignBaseline          垂直基线对齐

 

Layout.margins : real

Layout.bottomMargin : real

Layout.topMargin : real

Layout.rightMargin : real

Layout.leftMargin : real

        设置布局内元素的外边距,默认为0。如果设置了边距,那么元素的有效单元格大小将随着边距的增加而增加。

Layout.maximumHeight : real

Layout.maximumWidth : real

Layout.minimumHeight : real

Layout.minimumWidth : real

Layout.preferredHeight : real

Layout.preferredWidth : real

Layout.fillHeight : bool

Layout.fillWidth : bool

        设置布局内元素的宽高和是否填充策略。

Layout.column : int

Layout.row : int

        指定元素在网格布局GridLayout中的位置。

Layout.columnSpan : int

Layout.rowSpan : int

        指定元素在网格布局GridLayout中的行和列跨度,默认值为1,也就是占据一行一列。     

        在下面的例子中,第一个元素的行跨度为2,所以它占据了两行,在实际的应用场景中,该属性在网格布局中是非常好用的。

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.VirtualKeyboard 2.15
import QtQuick.Layouts 1.15

Window {
    id: window
    width:800
    height: 600
    visible: true
    title: qsTr("Hello World")

    GridLayout{
        anchors.centerIn: parent
        columns: 3
        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillHeight: true
            Layout.rowSpan: 2
            color: "blue"
            Layout.preferredWidth: 70
            Layout.preferredHeight: 40
        }

        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillHeight: true
            color: "blue"
            Layout.preferredWidth: 70
            Layout.preferredHeight: 40
        }

        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillHeight: true
            color: "blue"
            Layout.preferredWidth: 70
            Layout.preferredHeight: 40
        }
        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillHeight: true
            color: "blue"
            Layout.preferredWidth: 70
            Layout.preferredHeight: 40
        }
        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillHeight: true
            color: "blue"
            Layout.preferredWidth: 70
            Layout.preferredHeight: 40
        }
    }

}

三、行布局模块RowLayout、列布局模块ColumnLayout

        这两种模块的作用比较单一,只支持一行/一列的元素的布局,通过Layout类的依附属性完成界面的设计。

3.1 属性

layoutDirection : enumeration

        此属性保留列布局的布局方向是从左到右还是从右到左进行布局,默认为从左到右,在开头实例布局方向,左侧为从右到左效果,右侧为从左到右效果。

spacing : real

        该属性每个单元格之间的间距,默认为5。

四、网格布局模块GridLayout

        网格布局是单向布局的高级版本,代码参考第二个实例。

4.1属性

        layoutDirection属性与单向布局相同,其他属性还有:

columnSpacing : real

rowSpacing : real

        每个单元格之间的行列间距,默认为5。

flow : enumeration

        该属性表示没有显式单元格位置集的项的流向。它与columns或rows属性一起使用,它们分别指定何时将流重置为下一行或下一列,默认为从左到右。

        下面的实例中,通过五个不同颜色的

GridLayout{                             
    anchors.centerIn: parent            
    columns:2                           
   // rows: 3                           
   // flow:GridLayout.TopToBottom       
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        Layout.rowSpan: 2               
        color: "blue"                   
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
                                        
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "red"                    
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
                                        
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "green"                  
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "grey"                   
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "black"                  
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
}                                       

GridLayout{                             
    anchors.centerIn: parent            
   // columns:2                         
    rows: 3                             
    flow:GridLayout.TopToBottom         
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        Layout.rowSpan: 2               
        color: "blue"                   
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
                                        
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "red"                    
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
                                        
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "green"                  
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "grey"                   
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
    Rectangle {                         
        Layout.alignment: Qt.AlignBottom
        Layout.fillHeight: true         
        color: "black"                  
        Layout.preferredWidth: 70       
        Layout.preferredHeight: 40      
    }                                   
}                                       

columns : int

rows : int

         限制创建的行和列数,要注意元素超出范围,否则会崩溃,默认不限制。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1285543.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

有趣的代码——有故事背景的程序设计2

有趣的代码是很多的,所以接着上一篇,这一篇再和大家分享一些有故事背景的程序设计。 目录 1.百元买百鸡问题 2.哥德巴赫猜想 3.折半查找 4.主对角线元素之和 5.戈尼斯堡七桥问题 1.百元买百鸡问题 已知公鸡5元一只,母鸡3元一只&#xf…

【代码随想录刷题】Day20 二叉树06

文章目录 1.【654】最大二叉树1.1 题目描述1.2 解题思路1.3 java代码实现1.4 总结 2.【617】合并二叉树2.1 题目描述2.2 解题思路2.3 java代码实现 3.【700】二叉搜索树中的搜索3.1 题目描述3.2 解题思路3.3 java代码实现 4.【98】验证二叉搜索树4.1 题目描述4.2 解题思路4.3 j…

Hadoop学习笔记(HDP)-Part.02 核心组件原理

目录 Part.01 关于HDP Part.02 核心组件原理 Part.03 资源规划 Part.04 基础环境配置 Part.05 Yum源配置 Part.06 安装OracleJDK Part.07 安装MySQL Part.08 部署Ambari集群 Part.09 安装OpenLDAP Part.10 创建集群 Part.11 安装Kerberos Part.12 安装HDFS Part.13 安装Ranger …

Python代码编译并生成Docker镜像

Python代码编译并生成Docker镜像 前言 实际python项目交付时往往有针对关键代码进行保护的需求,本文介绍了一种简单可行的方案:1. 在Linux系统上先将 .py 文件编译为 .so 文件,2. 将整个项目打包成Docker镜像(解决 .so 文件的环…

Mars3d支持geoserver的rest服务类型数据渲染上图

需求:geoserver的rest服务类型的矢量数据通过mars3d的引擎直接渲染上图 学习过程: 1.通过全局查询示例的map.js文件,可以看到示例调用的rest服务类型,发现很多wfs接口的数据直接上图渲染矢量数据以及query接口下面调用这个服务的…

python-单词本|通讯录

编写程序,生词本。 def sayHello():print("" * 20 \n 欢迎使用生词本\n 1.查看生词本\n 2.背单词\n 3.添加新单词\n 4.删除单词\n 5.清空生词本\n 6.退出生词本\n * 20 \n)def addW(data):word input("请输入新单词:")trans i…

成为Java开发高手:掌握Spring框架的关键技能-DI

DI相关内容 1.1 setter注入1.1.2 注入引用数据类型1.1.3 注入简单数据类型步骤1:声明属性并提供setter方法步骤2:配置文件中进行注入配置步骤3:运行程序 1.2 构造器注入1.2.2 构造器注入引用数据类型步骤1:删除setter方法并提供构造方法步骤2:配置文件中进行配置构造方式注入步…

docker搭建xxl-job

使用docker-compose创建并运行xxl-job 查看、下载镜像 docker search xxl-job # 结果,自己指定版本 xuxueli/xxl-job-admin:2.3.1创建文件夹 /usr/local/software/xxl-job/logs编排docker-compose文件 version: 2 networks:wn_docker_net:external: true servic…

阿里云服务器租赁价格表,预算100元到5000元可选配置

阿里云服务器租用费用,阿里云轻量应用服务器2核2G3M带宽轻量服务器一年87元,2核4G4M带宽轻量服务器一年165元12个月,ECS云服务器e系列2核2G配置3M固定带宽99元一年、2核4G配置365元一年、2核8G配置522元一年,阿里云u1服务器2核4G、…

Hadoop学习笔记(HDP)-Part.10 创建集群

目录 Part.01 关于HDP Part.02 核心组件原理 Part.03 资源规划 Part.04 基础环境配置 Part.05 Yum源配置 Part.06 安装OracleJDK Part.07 安装MySQL Part.08 部署Ambari集群 Part.09 安装OpenLDAP Part.10 创建集群 Part.11 安装Kerberos Part.12 安装HDFS Part.13 安装Ranger …

如何通过DB操作地理空间数据

从公众号转载,关注微信公众号掌握更多技术动态 --------------------------------------------------------------- 一、PostgreSQL PostgreSQL是一个强大的对象关系数据库管理系统(ORDBMS)。它是在BSD风格的许可下发布的,因此是自…

2023年【危险化学品经营单位安全管理人员】免费试题及危险化学品经营单位安全管理人员复审考试

题库来源:安全生产模拟考试一点通公众号小程序 危险化学品经营单位安全管理人员免费试题是安全生产模拟考试一点通生成的,危险化学品经营单位安全管理人员证模拟考试题库是根据危险化学品经营单位安全管理人员最新版教材汇编出危险化学品经营单位安全管…

07、pytest指定要运行哪些用例

官方用例 # 目录结构 | |----test_mod.py | |----testing||----test_dir.py# content of test_mod.py import pytestdef func(x):return x 1def test_mod():print("test_mod function was invoked")assert func(3) 5def test_func():print("test_func was in…

Cyanine7-NHS ester荧光染料的化学结构、光谱性质和荧光特性

Cyanine7-NHS ester的结构包括一个靛菁环结构和一个NHS ester活性基团。NHS ester官能团是一种活化基团,用于将染料共价结合到含有游离氨基官能团的生物分子上。 **光谱性质:**Cyanine7-NHS ester的光谱性质通常包括: **激发波长&#xff08…

Hadoop学习笔记(HDP)-Part.16 安装HBase

目录 Part.01 关于HDP Part.02 核心组件原理 Part.03 资源规划 Part.04 基础环境配置 Part.05 Yum源配置 Part.06 安装OracleJDK Part.07 安装MySQL Part.08 部署Ambari集群 Part.09 安装OpenLDAP Part.10 创建集群 Part.11 安装Kerberos Part.12 安装HDFS Part.13 安装Ranger …

大佬齐聚首钢园,会碰撞出什么火花-百度APOLLO线下沙龙

陈老老老板🧙‍♂️ 👮‍♂️本文专栏:生活(主要讲一下自己生活相关的内容) 🤴本文简述:生活就像海洋,只有意志坚强的人,才能到达彼岸 👳‍♂️上一篇文章: 年度总结-你觉…

PyQt6 QTabWidget选项卡控件

​锋哥原创的PyQt6视频教程: 2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~共计37条视频,包括:2024版 PyQt6 Python桌面开发 视频教程(无废话…

11月,各地政府办公厅、市监局、不动产登记中心、财政厅持续深化电子签应用

本月,各地政府办公厅、市监局、不动产登记中心、财政厅、发改委、住建委、药品监管局等机关部门,持续推动电子印章、电子合同等功能在“政府采购、食品经营备案、不动产登记证书和证明、电子保函、工程项目招投标、实验室优化重组申报等”众多领域深化应…

C++入门第十一篇----多态

前言: 和前面的继承一样,多态也是对类和对象的功能进行扩展,以让其更加好用的一个知识点,接下来,就让我们总结一下多态,这个依托了继承的一个重要知识点。 对多态的理解和多态的概念: 何为多…

rvos 3编译与链接

做下面的两个练习需要: 在vmvb上装一个ubuntu会gcc、vi的基本使用 用vi写一个hello.cgcc -o hello.creadelf -h hello.oreadelf -S hello.oobjdump -S hello.o 用vi编辑一个test.cgcc -c test.creadelf -S test.o.text:代码 .data:初始化的全局变量和静态变量…