How can I figure out if the current item is the last item in a section of a ListView

Viewed 2410

So when I am drawing my ListView items I am sorting them into sections. Each item in a section has a line as a separator from the next item. For the last item I don't want to draw this separator. How can I figure out if the current item which is drawing itself, is the last item in the section (not in the ListView!)?

4 Answers

Each delegate item knows its own index so it can compare it against the ListView item count:

ListView {
    id: theListView
    model: ...
    delegate: Item {
        property bool isLast: index+1 < theListView.count ? false : true
        ...
    }
}

index == 0 shows the first(actually the latest) item of the model if the model is static and not dynamic. That means that if your listView is updated with the new items every now and then, then index == 0 gets bound to different element when the a new list item arrives.

Related