简体   繁体   中英

Hide an item (delegate) inside a ListView QML

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.

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM