QML Button & ListModel
-
Good morning to all, this is my first post, I'm not sure I'm in the right category but ...
I'm developing a program using a ListModel (and ListView) and I'm trying to establish a research function with this list. I have a Text Area where I can enter a name, and a button to search this name in the list. My function works but the problem is that it's not doing it in one go. To get my list sorted I have to push the button several times. Here is a part of my code :Rectangle {
id: rectTextName width: 300 height: 50 anchors.centerIn: parent border.color: "black" visible: false TextEdit { id: zoneTextName anchors.fill: parent font.pointSize : 11 text: "Entrer un nom" } Button { id: rechercher anchors.right: parent.right anchors.bottom: parent.bottom text: "Rechercher" onClicked: { var i = 0 while (i<listejoueurs.model.count && listejoueurs.model.get(i).playerName!==zoneTextName.text){ listejoueurs.model.remove(i,1) i++ } if (i<listejoueurs.model.count) { i++ } while (i<listejoueurs.model.count) { listejoueurs.model.remove(i,1) i++ } } } }
How to modify it so that when I push the button once, I get directly the sorted list?
I don't know if I was clear enough, not easy for me as beside being a beginner in QT/QML, I'm french ;)Thanks for you answers.
-
@Marine93 Well then you need a
SortFilterProxyModel
. It is based upon C++QSortFilterProxyModel
class. About the working you can find in this blog post. It shows forTableView
but it will work forListView
too. Basically it will for those views which usesListModel
. You can find examples in Qt installed directory on your system too underquick
directory.