QML(21)——Layout中的width, height设置技巧

本文介绍了如何在QtQuick的QML中控制组件的大小(size)和布局间的间距(spacing),包括固定大小与自适应策略,以及使用Layout.fillWidth和preferredWidth属性的应用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果展示

保持组件界面原始大小

在这里插入图片描述

组件size固定,spacing自适应

![在这里插入图片描述](https://img-blog.csdnimg.cn/bb5d6c8be658484983a2ddbf36b92e38.png

组件size自适应,spacing固定

在这里插入图片描述

使用技巧总结

优先级

Layout.fillWidth > preferredWidth > width > implicitWidth

需要固定size的自定义组件(button)

内部申明 width/height
外部不要设置Layout.fillWidth:true

需要自适应size的组件

界面设置 anchor.fill: parent
组件设置Layout.fillWidth:true

界面结构

主界面 main.qml

最外围界面

import QtQuick 2.12
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "./TestWidth"

Window {
    width: 1000
    height: 800
    visible: true
    title: qsTr("Hello World")

    LayoutSpacing{
        anchors.centerIn: parent
    }
}

功能界面 LayoutSpacing.qml

根节点是RowLayout ,子节点有

  • RowLayout : 2 Rectangle
  • Rectangle
  • ComboBox
  • BasicComponent :自定义组件
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    spacing: 10
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }

        Rectangle {
            color: 'plum'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }

    Rectangle {
        color: 'teal'
        Layout.fillWidth: true // 优先级 > preferredWidth > width
        Layout.preferredWidth: 100
        Layout.preferredHeight: 150
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    Rectangle {
        color: 'plum'
        Layout.fillWidth: true
        Layout.preferredWidth: 200
        Layout.preferredHeight: 100
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    
    ComboBox{
        id: basicCombobox
        // 在Layou里面时,width不生效, must use "Layout."
        Layout.preferredWidth: 60
        currentIndex: 0
        model: ["config", " spectrometer"]
        popup.closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
                           | Popup.CloseOnReleaseOutside
        onCurrentIndexChanged: {
            // 初始化时不会触发
            console.log("onCurrentIndexChanged", currentIndex)
            initCycle()
        }
        Component.onCompleted: {
            console.log("basicCombobox.width", basicCombobox.width)
        }
    }

    BasicComponent {
        // 如果内部,外部都没有指定size, 默认会为0
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
}

自定义组件 BasicComponent .qml

import QtQuick 2.15
import QtQuick.Layouts 1.15

Rectangle {
    id: root
    property color recColor: "#e9eed9"
//    implicitWidth: 30
//    implicitHeight: 50

    color: recColor
    width: 100
    height: 100
}

固定组件的size, Layout的spacing

如果想要展示组件的原始大小,不让其随着界面拉伸,同时固定spacing,可以注意以下

  • 对于LayoutSpacing.qml的外部和外部,都不要使用anchors.fill: parent
  • LayoutSpacing的内部根节点,不设定size,默认展示全部组件原始大小
    在这里插入图片描述

main.qml

import QtQuick 2.12
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "./TestWidth"

Window {
    width: 800
    height: 400
    visible: true
    title: qsTr("Hello World")

    LayoutSpacing{
        anchors.centerIn: parent
    }
}

LayoutSpacing.qml

import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    // 若对root的size不做任何设定,默认展示全部组件原始大小
    spacing: 10
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            //            Layout.minimumHeight: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
        。。。。。。

固定组件的size, spacing自动拉伸

如果外围界面比内部组件要大,但是希望固定住组件的原始大小,让spacing自动拉伸填充

  • LayoutSpacing设置anchors.fill: parent, 在外部内部都可
  • 界面内部组件不要设置 Layout.fillWidth: true
    在这里插入图片描述

LayoutSpacing.qml

import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    anchors.fill: parent
    spacing: 10


    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
//            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }

        Rectangle {
            color: 'plum'
//            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }

    Rectangle {
        color: 'teal'
//        Layout.fillWidth: true // 优先级 > preferredWidth > width
        Layout.preferredWidth: 100
        Layout.preferredHeight: 150
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    Rectangle {
        color: 'plum'
//        Layout.fillWidth: true
        Layout.preferredWidth: 200
        Layout.preferredHeight: 100
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }

    ComboBox{
        id: basicCombobox
        Layout.preferredWidth: 60
        currentIndex: 0
        model: ["config", " spectrometer"]
        popup.closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
                           | Popup.CloseOnReleaseOutside
    }
    
    BasicComponent {
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
}

固定spacing, 组件的size自动拉伸

如果希望组件大小自适应,保证间距spacing固定

  • LayoutSpacing设置anchors.fill: parent, 在外部内部都可
  • 界面内部组件设置 Layout.fillWidth: true
    在这里插入图片描述
    在这里插入图片描述

LayoutSpacing.qml

import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    anchors.fill: parent
    spacing: 10
    
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }

        Rectangle {
            color: 'plum'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }

    Rectangle {
        color: 'teal'
        Layout.fillWidth: true // 优先级 > preferredWidth > width
        Layout.preferredWidth: 100
        Layout.preferredHeight: 150
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    Rectangle {
        color: 'plum'
        Layout.fillWidth: true
        Layout.preferredWidth: 200
        Layout.preferredHeight: 100
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }

    ComboBox{
        id: basicCombobox
        Layout.preferredWidth: 60
        currentIndex: 0
        model: ["config", " spectrometer"]
        popup.closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
                           | Popup.CloseOnReleaseOutside
    }
    
    BasicComponent {
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
}

设置内部单个组件的size

作为Layout成员的组件,设置它的size必须使用Layout的属性,比如

Layout.minimumWidth: 100
Layout.preferredWidth: 200
Layout.preferredHeight: 100
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
	    ComboBox{
	        id: basicCombobox
	        width: 10
	        currentIndex: 0
	        model: ["A", " B"]
	        Component.onCompleted: {
	            console.log("basicCombobox.width", basicCombobox.width)
	        }
	    }
    }
qml: basicCombobox.width 140

使用 Layout.preferredWidth属性,才能生效

    RowLayout {
        Layout.fillWidth: true
        spacing: 10
	    ComboBox{
	        id: basicCombobox
	        Layout.preferredWidth: 60
	        currentIndex: 0
	        model: ["A", " B"]
	        Component.onCompleted: {
	            console.log("basicCombobox.width", basicCombobox.width)
	        }
	    }
    }
qml: basicCombobox.width 60
### 设置或修改 Qt 中布局间距的方法 在 Qt 中,`QBoxLayout` 类提供了用于设置和获取布局内组件之间间距的功能。通过这些方法可以精确控制界面元素之间的间隔。 对于 `QBoxLayout` 布局而言,可以通过调用成员函数来设定整体的内部间距: - 使用 `setSpacing(int)` 方法定义所有相邻项目间的默认空间大小[^1]。 ```cpp // 创建一个新的水平盒布局并设置其项间间距为20像素 QHBoxLayout *hbox = new QHBoxLayout; hbox->setSpacing(20); ``` 除了全局调整外,还可以单独处理特定位置上的空白区域——即所谓的“弹簧”。这类弹性占位符被称为 `QSpacerItem`,允许开发者灵活地增加额外的空间而不必实际添加可见的小部件。 另外,在更复杂的场景下可能还需要考虑四个边缘(上、下、左、右)各自的留白量以及行列拉伸因子等属性。具体来说有如下几个参数可供配置[^3]: - `layoutLeftMargin`: 左侧边界到第一个子元素的距离; - `layoutTopMargin`: 上方边界至顶部首个项目的距离; - `layoutRightMargin`: 右侧边界与最右边物件之间的距离; - `layoutBottomMargin`: 底部边界同最后一排物体底部间的距离; - `layoutHorizontalSpacing`: 同一行里各对象横向相隔多远; - `layoutVerticalSpacing`: 不同行里的东西纵向错开多少; 值得注意的是,上述提到的一些特性并非直接由 `QBoxLayout` 提供而是继承自基类 `QLayout` 或者其他更高层次的对象模型中实现的。因此当操作具体的盒子型布局实例时可以直接访问它们而无需担心兼容性问题。 最后提醒一点,虽然这里主要讨论了基于 C++ 的 API 接口,但在 Python 绑定版本 PySide6/PyQt5 下同样适用相同的逻辑和技术细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值