最大似然列表视图的空间项目,以填补宽度

0

的问题

我有一个列表视图(横向)在我的最大似然含有一些固定的尺寸的要素。 我要项目的间隔,以填补entiew宽度的列表视图元。 因此,如果有更少的元素,我希望他们隔开更多。 基本上我需要的是完全一样 Layout.fillWidth = true 酒店的RowLayout但对于列表视图。

我可以数多少个项目,我有,然后减去总项目的宽度从列表视图的宽度,鸿沟的项目数和分配的间距,但它似乎太愚蠢做。 有没有办法做到这一自动就像在RowLayout?

enter image description here

或许我需要使用一些不同的东西从列表视图为这个? 像RowLayout但是,我可以指定我的名单数据模型的吗?

listview qml qt spacing
2021-11-16 13:34:02
1

最好的答案

1

你能完成你想要什么用 ListView你只需要调整间隔动态基础上有多少名代表。 这个例子将打破,如果你的各位代表不同大小的(因为这是基于只有在宽度的第一委托)、或者如果代表们累计超过宽度 ListView.

ListView {
    width: 500
    orientation: Qt.Horizontal
    model: 6
    spacing: {
        if (count > 0) {
            return (width - (itemAtIndex(0).width * count))/(count - 1)
        } else {
            return 0
        }
    }

    delegate: Rectangle {
        implicitHeight: 50
        implicitWidth: 50
        color: "red"
        border.width: 1
    }
}

6 delegates 4 delegates

ListView 可能不是最适当的容器,用于这项任务。 我这样说,是因为它具有一个建立在 ScrollView 和其他行为,这听起来像你不需要。 如果所有你需要的是一个简单的行一些相同大小的代表,我同意scopchanov,并相信一个 Repeater 内部 RowLayout 会是最好的选择。 这是一个简单的例子:

RowLayout {
    width: 500

    Repeater {
        model: 6
        delegate: Rectangle {
            implicitHeight: 50
            implicitWidth: 50
            color: "tomato"
            border.width: 1
            Layout.alignment: Qt.AlignHCenter // must be set to align the rectangles within their empty space
        }
    }
}

6 delegates 4 delegates

你可能会注意到,这将引入空白的左右,如果这些差距是不可接受的,可能需要设置 spacingRowLayout 在相同的方式 ListView 例,而不是。

2021-11-16 18:17:48

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................