QML ListView Filter Elements

I have a ListView table. I want to add some filter buttons to hide / show the elements of this table based on the type of element. The easiest way is to set the visible property of the element delegate. However, hidden items are still counted in listView.contentHeight or listView.visibleArea.heightRatio. As a result, these values ​​change during scrolling and affect the height and position of the scroll bar, it crashes, expands, jumps in any order.

Another problem is that in the listView element, if the element is selected, there is no way to find out its index, for example, it looks like it is in second place in the table, but in fact its index is higher due to invisible Items. It would be nice if invisible elements were not taken into account at all.

Please help solve this problem. Thanks to everyone.

ListView{ id: listView delegate: itemdelegate } Component{ id: itemdelegate Item{ visible: model.type === filteredType ? true: false } } 
+6
source share
3 answers

Add elements to the display model dynamically, fe

 filterButton.onClicked:{ for(var i = 0; i < myListModel.count;i++) { if(myListModel.get(i).desiredProperty == "desiredValue") myDisplayModel.append("prop1":"val1"); } } 
+2
source

I decided to use the QML element of the VisualDataModel to filter the ListView, and it works very well.

+2
source

Assuming the data comes from the C ++ side of your application, the correct way to do this is to use Qt QSortFilterProxyModel in your model.

If you are not using it yet, here is a document on using C ++ models with QML views

+1
source

Source: https://habr.com/ru/post/975663/


All Articles