Determining properties of an item in a ListView given other items in the ListView
-
Hi there,
Currently I am trying to implement highlighting for items in a ListView, where I place a border around each item, and remove the border between adjacent items.
I've created a component for highlighting, where I can control whether I want a top, bottom, left and right border for each item, but I am unable to set these borders according to the next/previous item in the list.
The example below is a basic rundown:ListView { model: myModel delegate: Item { property bool isChecked: false HighlightComponent { topBorderWidth: [read previous items isChecked property] ? 0 : 1 bottomBorderWidth: [read next items isChecked property] ? 0 : 1 } } }
Any advice is much appreciated,
Thanks! -
I think you will have to use https://doc.qt.io/qt-5/qml-qtquick-listview.html#itemAtIndex-method to get the Item before and after the current item. I think if it is not rendered it may return null. But if that is the case it should be off screen.
let beforeitem = <listviewid>.itemAtIndex(index-1) let afteritem = <listviewid>.itemAtIndex(index+1)
Probably need to do check with isChecked to trigger changes. Might need to be tricky and fire off events or create a signal that fires anytime any isChecked changes. You can use Qt.callLater to make sure the event is not called for every item all the time.