拉伸元素以包含所有子元素

发布于 2024-11-13 10:10:09 字数 816 浏览 2 评论 0原文

QML 如何自动拉伸元素以使其所有子元素都适合它?以及如何指定间距?例如,我想在文本周围有一个矩形。矩形应该有一些内部间距。

如果我写下以下内容,则矩形的大小为 0,0。

Rectangle {
    color: "gray"
    anchors.centerIn: parent;

    Text {
        text: "Hello"
    }
}

如果我尝试使用 Column 元素来修复它,如 如何使 QML 项目增长以适应内容?,然后我通过整个窗口/父级获得一列,

Column {
    anchors.centerIn: parent

    Rectangle {
        color: "gray"
        anchors.fill: parent
    }

    Text {
        anchors.centerIn: parent
        text: "Hello"
    }
}

编辑:

我还尝试使用 Flow 元素而不是 Column,但后来我在整个窗口/父窗口中得到了一行。

How is it possible in QML to automatically stretch element so that all its childs fit in it? And how to specify spacing? For example, I would like to have a rectangle around a text. The rectangle should have some internal spacing.

If I write the following then the rectangle has a size of 0,0.

Rectangle {
    color: "gray"
    anchors.centerIn: parent;

    Text {
        text: "Hello"
    }
}

If I try to fix it by using the Column element, as suggested in How to make QML items to grow to fit contents?, then I get a column through the whole window/parent,

Column {
    anchors.centerIn: parent

    Rectangle {
        color: "gray"
        anchors.fill: parent
    }

    Text {
        anchors.centerIn: parent
        text: "Hello"
    }
}

Edit:

I have also tried to use the Flow element instead of Column, but then I got a row through the whole window/parent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江挽川 2024-11-20 10:10:09

您可以使用 childrenRect< /a> 属性:

import QtQuick 2.0

Rectangle {
    width: 320
    height: 200

    Rectangle {
        color: "BurlyWood"
        anchors.centerIn: parent
        width: childrenRect.width + 20
        height: childrenRect.height + 20

        Text {
            id: hello
            x: 10
            y: 10
            text: "Hello"
        }

        Text {
            anchors.left: hello.right
            anchors.leftMargin: 10
            anchors.top: hello.top
            text: "World"
        }
    }
}

但是,请注意,在直接子级之一中结合使用 childrenRect 和使用 anchors.centerIn:parent 会产生有关绑定循环的警告。

You can use the childrenRect property for this:

import QtQuick 2.0

Rectangle {
    width: 320
    height: 200

    Rectangle {
        color: "BurlyWood"
        anchors.centerIn: parent
        width: childrenRect.width + 20
        height: childrenRect.height + 20

        Text {
            id: hello
            x: 10
            y: 10
            text: "Hello"
        }

        Text {
            anchors.left: hello.right
            anchors.leftMargin: 10
            anchors.top: hello.top
            text: "World"
        }
    }
}

However, note that using childrenRect in combination with using anchors.centerIn: parent in one of the direct children yields a warning about a binding loop.

怎会甘心 2024-11-20 10:10:09

手动设置 widthheight 可以,但有点难看:

Rectangle {
    color: "gray"
    width: label.width+20
    height: label.height+20
    anchors.centerIn: parent

    Text {
        id: label
        anchors.centerIn: parent
        text: "Hello"
    }
}

Setting the width and height manually works, but is a little ugly:

Rectangle {
    color: "gray"
    width: label.width+20
    height: label.height+20
    anchors.centerIn: parent

    Text {
        id: label
        anchors.centerIn: parent
        text: "Hello"
    }
}
彡翼 2024-11-20 10:10:09

我认为使用 chilrenRect 属性就足够了(正如 Thorbjørn Lindeijer 的建议)。它不会自动考虑子元素的所有各种边距。如果后者发生变化,根矩形不会自动调整其大小。我个人提出了以下解决方案:

Rectangle {
    color: "white"
    implicitWidth: row.implicitWidth + extraLeft + extraRight
    implicitHeight: row.implicitHeight + extraTop + extraBottom
    
    property int extraMargin: row.anchors.margins ? row.anchors.margins : 0
    property int extraTop: row.anchors.topMargin ? row.anchors.topMargin : extraMargin
    property int extraBottom: row.anchors.bottomMargin ? row.anchors.bottomMargin : extraMargin
    property int extraLeft: row.anchors.leftMargin ? row.anchors.leftMargin : extraMargin
    property int extraRight: row.anchors.rightMargin ? row.anchors.rightMargin : extraMargin
    
    
    RowLayout {
        id: row
        spacing: 50
        anchors.fill:parent
        anchors.margins: 50
        anchors.leftMargin: 100
        
        Label {
            text: "hello"
        }
        Label {
            text: "world"
        }
    }
}

I don't think using chilrenRect property is sufficient (as suggested by Thorbjørn Lindeijer). It doesn't automatically take into account all various margins of the child(ren) element(s). If the latter changes, the root rectangle doesn't automatically adjust its size. I personally came with the following solution:

Rectangle {
    color: "white"
    implicitWidth: row.implicitWidth + extraLeft + extraRight
    implicitHeight: row.implicitHeight + extraTop + extraBottom
    
    property int extraMargin: row.anchors.margins ? row.anchors.margins : 0
    property int extraTop: row.anchors.topMargin ? row.anchors.topMargin : extraMargin
    property int extraBottom: row.anchors.bottomMargin ? row.anchors.bottomMargin : extraMargin
    property int extraLeft: row.anchors.leftMargin ? row.anchors.leftMargin : extraMargin
    property int extraRight: row.anchors.rightMargin ? row.anchors.rightMargin : extraMargin
    
    
    RowLayout {
        id: row
        spacing: 50
        anchors.fill:parent
        anchors.margins: 50
        anchors.leftMargin: 100
        
        Label {
            text: "hello"
        }
        Label {
            text: "world"
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文