Qt 根据visible属性更改ListView的高度

Qt 根据visible属性更改ListView的高度,qt,qml,Qt,Qml,我有一个列表视图,其中项目是否可见取决于标题是否展开。当visible属性设置为false时,该区域中只有空格。如何删除列表中的空白,以便下一个可见项出现在视图中? 目前我已经设置了高度:model.count*50,其中50是每个项目的高度。我尝试每次减去50的一个条目可见/代码>属性被设置为false,但这只是降低了列表的高度,但空白仍然存在。 ListView { id: list focus: true anchors.top: header.bottom anc

我有一个
列表视图
,其中项目是否可见取决于标题是否展开。当
visible
属性设置为false时,该区域中只有空格。如何删除列表中的空白,以便下一个可见项出现在视图中? 目前我已经设置了
高度:model.count*50
,其中50是每个项目的高度。我尝试每次减去50的一个条目<代码>可见/代码>属性被设置为false,但这只是降低了列表的高度,但空白仍然存在。

ListView { id: list
    focus: true
    anchors.top: header.bottom
    anchors.left: parent.left
    anchors.right: parent.right
    width: parent.width
    height:model.count*50
    model:hall

    Component {
        id:comp
        Item{
        width: parent ? parent.width : 0
        height: parent ? parent.height: 0

        Column{
            id:col1
            width: parent.width - 16
            height: 25
            anchors.centerIn: parent

            spacing: 3

            Text { id: name
                width: parent.width
                text: name
                elide: Text.ElideMiddle
                font: "black"
                color: "grey"

            }
            Row{
                width: parent.width

                Text { id: file1
                    width: parent.width * 0.6 - 5
                    text: "hello"
                    horizontalAlignment: Text.AlignLeft
                    elide: Text.ElideMiddle
                    font: systemFont.TableCellFont
                }
                Text { id: file2
                    width: parent.width*0.4 - 3
                    horizontalAlignment: Text.AlignRight
                    text: mod
                    font: systemFont.TableCellFont
                }
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.warn("Mouse onClicked")
                
            }
        }
    }
    }

    delegate: Item {
        width:  parent.width;
        height: {
            if (!cric && expanded)
                return 50
            else
                return 0
        }

        Loader {
            anchors.fill: parent
            sourceComponent: comp
            height: expanded? 50: 0
            readonly property int index:model.index
            readonly property string mod: model.mod
            visible: !cric && expanded
        }
    }

    Keys.onReturnPressed: {
            console.warn("Return pressed") 
        }
    }

创建空白是因为通过将visible设置为false而隐藏的列表项仍设置了其维度。您可以将项目高度更新为0,空白空间将不可见。请尝试此示例以供参考

import QtQuick 1.1

Rectangle{
    width: 400; height: 400
    color: 'lightblue'

    ListView{
        width: 400; height: count * 30
        model: 20
        delegate: Rectangle{
            id: rectDel
            width: 200; height: 30
            color: 'lightgreen'
            border{
                width: 1
                color: 'black'
            }

            Text{
                text: index
                anchors.centerIn: parent
            }

            Rectangle{
                width: 20; height: 20
                radius: 10
                color: 'red'
                anchors{
                    verticalCenter: parent.verticalCenter
                    right: parent.right; rightMargin: 10
                }
                Text{ text: 'X'; anchors.centerIn: parent; }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        rectDel.visible = false
                        rectDel.height = 0
                    }
                }
            }
        }
    }
}


这是在删除空白空间的情况下,但是当我使用键来选择项目时,也会选择不可见的项目。当我按下返回键时,屏幕上看不到的项目就会打开。有两种方法可以解决这个问题。更新列表视图的模型(不包括不可见项)。或者,当您按键选择项目时,通过检查当前项目是否可见来处理列表视图中的按键/释放事件,并相应地更新列表视图当前索引。是的,我现在正在做的就是处理按键。我只是觉得有更好的方法。我想不会,谢谢。