QML ListView,滚动时如何使列表元素不与标题重叠

时间:2016-09-07 21:52:31

标签: qt qml qtquick2 qt-quick

我有一个启用了头部委托的ListView。我有一个标题定位属性设置为“OverlayHeader”。滚动元素时,标题将保持不变。但是,元素将与标题重叠。有没有办法防止这种情况发生。

与标题重叠的列表元素示例。

import QtQuick 2.5

Rectangle {
    width: 360; height: 600

  ListView {

    width: 350; height: 200
    anchors.centerIn: parent
    id: myList
    model: myModel
    highlight: highlightBar
    clip: true

    snapMode: ListView.SnapToItem

    headerPositioning: ListView.OverlayHeader

    header: Rectangle {
      id: headerItem
      width: myList.width
      height:50

      color: "blue"

      Text {
        text: "HEADER"
        color: "red"
      }
    }

    delegate: Item {
      id: delegateItem
      width: 400; height: 20
      clip: true
      Text { text: name
      }

      MouseArea {
        id: mArea
        anchors.fill: parent
        onClicked: { myList.currentIndex = index; }
      }
    }

  }

  Component {
    id: highlightBar
    Rectangle {
      width: parent.width; height: 20
      color: "#FFFF88"
    }
  }

  ListModel {
      id: myModel
  }

  /* Fill the model with default values on startup */
  Component.onCompleted: {
      for(var i = 0; i < 100; i++) {
          myModel.append({ name: "Big Animal : " + i});
      }
  }
}

1 个答案:

答案 0 :(得分:4)

header's default z value is 1,因此您可以将其设置为更高的值,以确保它超过代理人:

import QtQuick 2.5

Rectangle {
    width: 360
    height: 600

    ListView {

        width: 350
        height: 200
        anchors.centerIn: parent
        id: myList
        model: myModel
        highlight: highlightBar
        clip: true

        snapMode: ListView.SnapToItem

        headerPositioning: ListView.OverlayHeader

        header: Rectangle {
            id: headerItem
            width: myList.width
            height: 50
            z: 2

            color: "blue"

            Text {
                text: "HEADER"
                color: "red"
            }
        }

        delegate: Item {
            id: delegateItem
            width: 400
            height: 20
            Text {
                text: name
            }

            MouseArea {
                id: mArea
                anchors.fill: parent
                onClicked: {
                    myList.currentIndex = index
                }
            }
        }
    }

    Component {
        id: highlightBar
        Rectangle {
            width: parent.width
            height: 20
            color: "#FFFF88"
        }
    }

    ListModel {
        id: myModel
    }

    /* Fill the model with default values on startup */
    Component.onCompleted: {
        for (var i = 0; i < 100; i++) {
            myModel.append({
                name: "Big Animal : " + i
            })
        }
    }
}

请注意clipping view delegates is bad for performance