【问题标题】:Qml ListView empty rows placeholders like in TableViewQml ListView 空行占位符,如 TableView
【发布时间】:2016-03-29 10:30:19
【问题描述】:

我希望ListView 显示空行直到其高度结束。喜欢TableView,但使用ListView。

这可能吗?

【问题讨论】:

    标签: qt listview qml qtquick2 qtquickcontrols


    【解决方案1】:

    在 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;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      一个非常简单的方法。

      首先,定义项目高度:

      property int itemHeight: 40
      

      然后,在ListView:

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

      其中mainForm 是listview1 的父级。

      为了正确显示空行,您可以检查 item 属性的存在,即:

      color: colorCode ? colorCode : "white"
      
      text: name ? name : ""
      

      【讨论】:

      • 好吧,这并不是真正的解决方案。项目可能有可变的高度,如果我们需要删除空行,我们每次添加新元素时都需要检查。
      • 这不是一个完整的解决方案,但你的答案也不是很完整:)
      猜你喜欢
      • 1970-01-01
      • 2018-03-21
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2022-01-19
      • 2012-09-10
      相关资源
      最近更新 更多