QtQuick 布局管理-布局管理器

50 篇文章 0 订阅

布局管理器

QtQuick布局管理器(Layouts)是一组在用户界面中用于排列项目的类型。
与定位器不同,布局管理器不仅进行布局,而且会改变项目的大小,所以更适用于需要改变用户界面大小的应用。

因为布局管理器也继承自Item,所以她们可以嵌套。

QtQuick布局管理器类似于传统 Qt Widgets应用的布局管理器。使用时需要导入:import QtQuick.Layouts

布局管理器主要包括:

  1. RowLayout
  2. ColumnLayout
  3. GridLayout
  4. StackLayout

见Qt帮助文档:Qt Quick Layouts Overview

1. 主要特色

  1. 项目的对齐方式Layout.alignment指定,主要有:
    1. Qt::AlignLeft
    2. Qt::AlignHCenter
    3. Qt::AlignRight
    4. Qt::AlignTop
    5. Qt::AlignVCenter
    6. Qt::AlignBottom
    7. Qt:AlignBaseline
  2. 可变大小的项目使用Layout.fillWidthLayout.fillHeight属性指定,当将其值设为true时会根据约束条件变宽或变高。
  3. 大小约束可以通过Layout.minimumWidthLayout.preferedWidthLayout.maximumWidth(包括height对应的)来指定。
  4. 间距可以通过spacingrowSpacingcolumnSpacing属性指定。

除了上面的,GridLayout还有如下特色:

  1. 网格中的坐标可以通过Layout.rowLayout.column指定。
  2. 自动网格坐标同时使用了flowrowscolumn属性。
  3. 行或列的跨度可以通过Layout.rowSpanLayout.columnSpan属性来指定。

2. 大小约束

要想使得一个项目可以通过布局管理器调整大小,需要指定其:

  1. 最小宽高(minimumWidth, MinimumHeight
  2. 最佳宽高(preferedWidth, preferedWidth
  3. 最大宽高(maximumWidth, maximumHeight

并将对应的Layout.fillWidthLayout.fillHeight设置为true。

例:

在一个布局管理器中横向排列两个矩形,当拉伸程序窗口时,左边距形可以从50x150变化到300x150,右边矩形可以从100x150变化到∞x100

import QtQuick
import QtQuick.Layouts

Window{
    width: 400
    height: 300
    visible: true
    
    RowLayout{
        id: layout
        anchors.fill: parent
        spacing: 6
        Rectangle{
            color: "lightgrey"
            
            Layout.fillWidth: true
            Layout.minimumWidth: 50
            Layout.preferredWidth: 100
            Layout.maximumWidth: 300
            
            Layout.minimumHeight: 150
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
        
        Rectangle{
            color: "black"
            Layout.fillWidth: true
            Layout.minimumWidth: 100
            Layout.preferredWidth: 200
            
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
                color: "white"
            }
        }
    }
}

在这里插入图片描述
有效的最佳(preferred)属性值可能来自几个候选属性。要决定有效的最佳属性,会对这些候选属性以下面的顺序进行查询,使用第一个有效的值:

  1. Layout.preferredWidthLayout.preferredHeight
  2. implicitWidhtimplicitHeight
  3. widthheight

一个项目可以仅指定Layout.preferredWidth而不指定Layout.preferredHeight,此时,有效的最佳高度会从implicitHeight或最终的height中选取。

为了将布局管理器与窗口进行关联,可以为布局管理器添加锚anchors.fill,确保布局管理器能够跟随窗口一起改变大小。

布局管理器的大小约束可以用来确保窗口大小不会超过约束条件,还可以将布局管理器的约束设置到窗口项目的minimumWidthminimumHeightmaximumWidthmaximumHeight等属性。

例如下面的代码:

Window{
    width: 400
    height: 300
    visible: true
    
    minimumWidth: layout.Layout.minimumWidth
    minimumHeight: layout.Layout.minimumHeight
    maximumWidth: 1000
    maximumHeight: layout.Layout.maximumHeight
    
    RowLayout{
        ...
    }
}

实际编程中,通常希望窗口的初始化大小可以是布局管理器的隐含(implicit)大小,那么就可以这样来设置:

Window{
	width: layout.implicitWidth
	height: layout.implicitHeight
	
	RowLayout{ ... }
}

3. StackLayout

StackLayout栈布局管理器可以管理多个项目,但是只能显示一个项目。

可以通过currentIndex属性来设置当前显示的项目,索引号对应布局管理器中子项目的顺序,从0开始。

另外,StackLayout还包含index和isCurrentItem等附加属性。

例如:

import QtQuick
import QtQuick.Layouts

Window{
    width: 640
    height: 480
    visible: true
    
    StackLayout{
        id:layout
        anchors.fill: parent
        currentIndex: 1
        
        Rectangle{
            color: "plum"
            implicitWidth: 300
            implicitHeight: 200
        }
    }
    
    MouseArea{
        anchors.fill: parent
        onClicked: {
            if(layout.currentIndex === 1)
                layout.currentIndex = 0;
            else
                layout.currentIndex = 1
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

barbyQAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值