8
votes

Is there a way to hide a particular item on some event in a ListView?

So far I can do it by setting visible to false and height to zero of a delegate.

But If i have spacing in a listView set to 2 for example it appears that this solution is broken.

enter image description here

2
You should either implement spacing as a part of a delegate or use QSortFilterProxyModel - folibis
Yes, definitely put spacing inside the delegate, the proxy is overkill IMO. - dtech
It depends on the number of delegates needed to be hidden. - GrecKo
@folibis stoped on setting height = 0 - spacing - Mikhail
A cleaner solution in my opinion would be that the model itself updates and remove the items you don't want to display (not the data from memory, but the elements from the model). It's more work, but in case you display this list in different views it gets much more robust and easy to manage. - ymoreau

2 Answers

1
votes

I think the proper way is to use a proxy model that filters out the elements that should not be displayed. You can use a QSortFilterProxyModel or implement your own QAbstractProxyModel. With that it is even possible to animate the removal and addition of the elements.

Or use SortFilterProxyModel if you don't wanna touch C++ and performance is not a problem

0
votes

A hack around this could be to set the spacing of the ListView to 0 and implement it in the delegate itself. Something like this:

ListView{
   id: listView
   spacing: 0
   delegate: Item{
      id: itemDelegate
      width: parent.width; height: spacingRect.height + actualDelegate.height
      Item {id: actualDelegate;} // your actual delegate
      Rectangle{ id: spacingRect; height: 2; width: parent.width; color: "transparent"; anchors.top: actualDelegate.bottom}
   }
}

In this way when you hide the delegate the spacing will also be hidden