4

I am building a Qt5 application based on Qt-Quick 2 for the UI. I have an issue while displaying a ListView with an highlight component. When I scroll the ListView the highlight rectangle is visible outside of the ListView and I can't find a way to avoid it.

Here is an example of the issue with a minimal QML file:

import QtQuick 2.0

Rectangle {
    width: 360; height: 600

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

    delegate: Item {
      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});
      }
  }
}

Is there a way to "limit" a component to its parent borders or to hide the highlight component while scrolling?

4

1 回答 1

5

正如文档所报告的:

注意:视图不会自动启用剪辑。如果视图没有被其他项目或屏幕剪辑,则需要设置 clip: true 以便很好地剪辑视图外的项目。

因此,您遇到的是一种常见行为,您应该 1) 通过其他 s 剪辑视图(例如,使用或简单地将属性设置为Item的页眉Rectangle和页脚,即Rectanglez:infinitecliptrue

ListView{
   //...
   clip:true
   //...
}

Clipping 有一些性能缺点,随着应用程序的增长,这些缺点会极大地影响应用程序。因此,应该仔细评估它的使用,尤其是在视图场景之外。

于 2013-06-19T12:48:59.280 回答