Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.



  • I forgot to precise that when I say it doesn't do it in one go, I mean that the items to be eliminated are eliminated bit by bit, as and when I push the button again and again, and not all at the same time.


  • Moderators

    @Marine93 Are you trying to sort the ListModel ?



  • @p3c0 Not really, I'm trying to do a research in the list according to some criterias and I only want to keep the items of the list that respect these criterias


  • Moderators

    @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 for TableView but it will work for ListView too. Basically it will for those views which uses ListModel. You can find examples in Qt installed directory on your system too under quick directory.


Log in to reply