簡體   English   中英

在 ListView QML 中隱藏項目(委托)

[英]Hide an item (delegate) inside a ListView QML

有沒有辦法在 ListView 中的某個事件上隱藏特定項目?

到目前為止,我可以通過將委托的visible設置為false並將height設置為零來實現。

但是,例如,如果我將 listView 中的間距設置為 2,則此解決方案似乎已損壞。

在此處輸入圖片說明

我認為正確的方法是使用代理模型過濾掉不應該顯示的元素。 您可以使用QSortFilterProxyModel或實現您自己的QAbstractProxyModel 有了它,甚至可以對元素的刪除和添加進行動畫處理。

或者如果您不想接觸 C++ 並且性能不是問題,請使用SortFilterProxyModel

解決這個問題的一個技巧可能是將ListView的間距設置為 0 並在委托本身中實現它。 像這樣的東西:

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}
   }
}

這樣,當您隱藏委托時,間距也將被隱藏

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM