简体   繁体   English

QML 布局:如何为行或列布局中的项目赋予权重?

[英]QML Layouts: How to give weights to items in a row or column layout?

I'm trying to figure out a way to layout items proportionally by specifying a kind of weight for each item.我试图通过为每个项目指定一种权重来找出按比例布局项目的方法。 For example the way Android does their layouts .例如,Android 的布局方式

The way I'm trying to achieve it is like so:我试图实现它的方式是这样的:

import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3

GridLayout {
    columns: 4
    width: 640
    height: 480

    Rectangle {
        color: "red"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.columnSpan: 1
    }
    Rectangle {
        color: "#80000000"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.columnSpan: 2
    }
    Rectangle {
        color: "blue"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.columnSpan: 1
    }
}

I would expect the width of the middle rectangle to be the sum of the other two rectangles, but instead they are all equal widths.我希望中间矩形的宽度是其他两个矩形的总和,但它们的宽度都是相等的。

Using relational bindings on the Layout attached properties seems to always lead to weird binding loops.在 Layout 附加属性上使用关系绑定似乎总是会导致奇怪的绑定循环。 I know I could just use a Row instead with relational bindings, but I'd prefer to use Layouts if possible.我知道我可以只使用 Row 代替关系绑定,但如果可能的话,我更喜欢使用 Layouts。

EDIT编辑

This seems to work the way I want it to, but I don't know why it works.这似乎按照我想要的方式工作,但我不知道为什么会这样。 It behaves as if the preferredWidth value is the weight of the item.它的行为就像preferredWidth值是项目的重量一样。

import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3

RowLayout {
    width: 640
    height: 480

    Rectangle {
        color: "red"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.preferredWidth: 1
    }
    Rectangle {
        color: "#80000000"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.preferredWidth: 2
    }
    Rectangle {
        color: "blue"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.preferredWidth: 1
    }
}

Not sure if intentional or not but Layout.preferredWidth (or Layout.preferredHeight for ColumnLayouts) can be used as a "weight".不确定是否有意,但Layout.preferredWidth (或Layout.preferredHeight for ColumnLayouts)可以用作“权重”。 Things break when also specifying Layout.minimumWidth , but I don't think it makes much sense to be specify minimum dimensions when trying to implement layouts in terms of weights anyways.当还指定Layout.minimumWidth ,事情会中断,但我认为在尝试根据权重实现布局时指定最小尺寸没有多大意义。

import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3

RowLayout {
    width: 640
    height: 480

    Rectangle {
        color: "red"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.preferredWidth: 1
    }
    Rectangle {
        color: "#80000000"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.preferredWidth: 2
    }
    Rectangle {
        color: "blue"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Layout.preferredWidth: 1
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM