【问题标题】:QML - How to correctly set an implicitWith to a GridLayoutQML - 如何正确地将隐式设置设置为 GridLayout
【发布时间】:2019-05-10 02:18:44
【问题描述】:

我正在使用 Qml 5.12 并且基本上试图将隐式宽度设置为 GridLayout。

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

红色矩形适合 GridLayout,因此我可以看到 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没有考虑到。

谁能告诉我为什么?

谢谢!

【问题讨论】:

  • 如果没有设置width,我猜GridLayout会根据内容调整其大小。来自 Qt 文档:implicitWidth :如果未指定宽度或高度,则定义项目的自然宽度或高度。但是GridLayout 隐式设置了它的大小,因此不考虑_implicitWidth
  • 是的,看起来像。感谢您的信息。

标签: qml grid-layout


【解决方案1】:

GridLayout 根据其子级的implicitWidth 计算自己的implicitWidth。所以你设置的值会被计算的值覆盖。

implicitWidth 是 Item 想要拥有的宽度(如果没有明确设置 width,它将拥有的宽度)。将其设置为基于其子级以外的其他内容或某些内部值是没有意义的。

在这里,您希望 GridLayout 的大小与您的 Rectangle 完全相同,因此只需设置其 width 属性即可。

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多