QML-如何正确地将一个implicitWith设置为一个GridLayout [英] QML - How to correctly set an implicitWith to a GridLayout

查看:95
本文介绍了QML-如何正确地将一个implicitWith设置为一个GridLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Qml 5.12,基本上是尝试将GridLayout的隐式宽度设置为.

I'm using Qml 5.12 and basically trying to set an implicitWidth to a GridLayout.

为此,我有一个紫色矩形,并将矩形的宽度设置为GridLayout.

For that, I have a purple rectangle and set the rectangle's width to the GridLayout.

红色矩形适合GridLayout,因此我可以看到GridLayout的宽度.

The red rectangle fit with the GridLayout so I can see the width of my GridLayout.

这是我的代码:

Rectangle { anchors.fill: gl; color: "red"; opacity: 0.22 }
Rectangle { id: rect; width: 350; height: 30; color: "purple"; }

GridLayout
{
    id: gl
    y: 35
    implicitWidth: rect.width
    columns: 2
    Label { text: "This is a test" }
    SpinBox { Layout.alignment: Qt.AlignRight }
}

如果我运行代码,我希望两个矩形都具有相同的宽度.但是实际结果是红色矩形较小.因此,implicitWidth并不合理.

If I run the code, I expect to have my both rectangle with the same width. But the actual result is that the red rectangle is smaller. So the implicitWidth was not considerate.

有人可以告诉我为什么吗?

Can anybody tell my why ?

谢谢!

推荐答案

GridLayout 根据其子级的 implicitWidth 计算自己的 implicitWidth .因此,您设置的值将被计算出的值覆盖.

The GridLayout compute its own implicitWidth based on its children's implicitWidth. So the value you set gets overwritten by the computed one.

implicitWidth 是项目要具有的宽度(如果未明确设置 width ,则它将具有该宽度).根据其子级以外的其他值或某些内部值进行设置几乎没有任何意义.

implicitWidth is the width an Item wants to have (and the one it would have if no width is explicitely set). Setting it based on something else than its children or some internal value makes little sense.

在这里,您希望GridLayout为您的 Rectangle 的确切大小,因此只需设置其 width 属性即可.

Here you want the GridLayout to be the exact size of your Rectangle so just set its width property.

这篇关于QML-如何正确地将一个implicitWith设置为一个GridLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆