首页 > 解决方案 > QML ColumnLayout 不尊重 minimumHeight

问题描述

我总是对 QML 的布局管理有问题。因此,我创建了一个非常简单的 QML App 来检查 ColumnLayout。如果我水平缩小窗口,当达到 100 时它将停止在 minimumWitdh 处。但是如果我垂直缩小窗口,我可以缩小窗口,这样两个矩形都会消失。

出了什么问题?为什么不尊重 minimumHeight ?

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

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    ColumnLayout {
        Rectangle {
            color: "red"
            Layout.minimumWidth: 100
            Layout.minimumHeight: 100
        }
        Rectangle {
            color: "blue"
            Layout.minimumWidth: 100
            Layout.minimumHeight: 100
        }
    }
}

标签: qtlayoutqml

解决方案


福尔比斯可能是对的。但我找到了另一个解决方案。似乎 minimumWidth 和 minimumHeight 将计算 ColumnLayout 的隐式宽度和隐式高度。因此,这又可以用作窗口的 minimumWidth 和 minimumHeight,如下所示:

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    minimumHeight: col.implicitHeight
    minimumWidth: col.implicitWidth


    ColumnLayout {
        id: col

        Rectangle {
            color: "red"
            Layout.minimumWidth: 100
            Layout.minimumHeight: 100
        }
        Rectangle {
            color: "blue"
            Layout.minimumWidth: 100
            Layout.minimumHeight: 100
        }
    }
}

当您来自 XAML 世界时,这很奇怪。


推荐阅读