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?

koopajah
  • 23,792
  • 9
  • 78
  • 104

1 Answers1

5

As reported by the documentation:

Note: Views do not enable clip automatically. If the view is not clipped by another item or the screen, it will be necessary to set clip: true in order to have the out of view items clipped nicely.

Hence, what you are experiencing is a common behaviour and you should either 1) clip the view via other Items (e.g. a header Rectangle and a footer Rectangle with z:infinite or simply set the clip property to true, i.e.

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

Clipping has some perfomance disavantages which can greatly affect the application as it grows. Hence, its usage, especially outside the views scenario, should be evaluated carefully.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Amit Tomar
  • 4,800
  • 6
  • 50
  • 83
  • 2
    It seems evident now but I've spent a day trying as many properties as I could find and totally missed this one, so thanks a lot!! – koopajah Jun 19 '13 at 13:04
  • 3
    @koopajah Happens to best of us :) Just a suggestion : When you are not able to find a particular property, try looking into the `item` element of qml. Most probably you will find it. – Amit Tomar Jun 19 '13 at 17:10
  • This property should be `true` by default. I've wasted 2h because of this :( – Abdelilah El Aissaoui Nov 20 '16 at 12:56