简体   繁体   English

Qml ListView空行占位符,如TableView

[英]Qml ListView empty rows placeholders like in TableView

I want ListView to display empty rows till the end of its height.我希望ListView显示空行直到其高度结束。 Like TableView but with ListView .TableView但有ListView

Is that possible?那可能吗?

在此处输入图片说明

With help of Qt guys, got a nice solution (this code is inside of ListView which is inside of ScrollView):在 Qt 的帮助下,得到了一个很好的解决方案(这段代码在 ListView 里面,它在 ScrollView 里面):

Component {
    id: rectComp
    Rectangle {
        id: rowHeaderRect
        height: 50
        color:  rowIndex % 2 == 0 ? Theme.rowColor : Theme.altRowColor
    }
}
Column {
    id: rowfiller    
    Loader {
        id: rowSizeItem
        sourceComponent: rectComp
        visible: false
    }
    property int rowHeight: rowSizeItem.implicitHeight
    property int paddedRowCount: height/rowHeight

    y: listview.contentHeight - listview.contentY + listview.originY
    width: parent.width
    visible: true
    height: scrollview.viewport.height - listview.contentHeight
    Repeater {
        model: visible ? parent.paddedRowCount : 0
        Loader {
            property int rowIndex: index
            width: rowfiller.width
            height: rowfiller.rowHeight
            sourceComponent: rectComp;
        }
    }
}

A very simple approach.一个非常简单的方法。

First, define the item height:首先,定义项目高度:

property int itemHeight: 40

Then, on the ListView :然后,在ListView

Component.onCompleted: {
            var numItems = mainForm.height / itemHeight
            for(var i=0; i < numItems; i++) {
                listView1.model.append({})
            }
        }

where mainForm is the parent of listview1 .其中mainFormlistview1的父级。

For the empty rows to display correctly you could check for the existence of the item property, ie:为了正确显示空行,您可以检查 item 属性是否存在,即:

color: colorCode ? colorCode : "white"

text: name ? name : ""

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

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