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

Dynamically add combo box without resetting the previous selection



  • Please help me with this. I am looking to dynamically add more Combobox to my view. The code works fine and adds a new Combobox on the click of the button.

    However, when I add a new Combobox, the data in existing Comboboxes are reset to default. How do I just append another Combobox without resetting the earlier selected values. I will prefer not to save the existing values in some variable

    Here is my code

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Layouts 1.3
    
    Page {
    
        id : somepageid
        property int modelList: 0
        property int counter: 0
    
        // Combobox Listmodel
        ListModel{
            id: listmodel
    
            ListElement{
                elem: "A"
            }
            ListElement{
                elem: "B"
            }
            ListElement{
                elem: "C"
            }
        }
    
        // Add more item to Listview
        function addMore(){
            if(counter < listmodel.count){
                counter++
                modelList++
                listid.model = modelList
            }
        }
    
        // Button
        Button{
            id: testButton
            text: "Click to add Combobox"
            onClicked: {
                addMore()
            }
        }
    
        // Listview
        ListView{
            id: listid
            model: modelList
            anchors.top:  testButton.bottom
            height: listid.model * 40
    
            delegate: Row{
                ComboBox{
                    id: combo
                    textRole: "elem"
                    model:listmodel
                }
            }
        }
    }
    


  • @chilarai hi

    ApplicationWindow {
        visible: true
        width: 400
        height: 550
        title: qsTr("Ttile")
    
        ListModel {
            id: listModel
        }
    
        ListView{
            model: listModel
            anchors.top:  testButton.bottom
            height: parent.height
    
            delegate: Row{
                ComboBox{
                    textRole: "elem"
                    model:  ListModel{
                        ListElement{
                            elem: "A"
                        }
                        ListElement{
                            elem: "B"
                        }
                        ListElement{
                            elem: "C"
                        }
                    }
                    onCurrentIndexChanged: listModel.setProperty(index ,"value", currentIndex)
                }
            }
        }
    
    
        Button{
            id: testButton
            text: "Click to add Combobox"
            onClicked: {
                listModel.append({"value":0})
            }
        }
    }
    


  • @LeLev Thanks once again. Have been trying this for long.



  • @chilarai
    Hi @chilarai . I note that most (if not all) of your queries are about QML/QtQuick. Where that is the case (like this one), it would be better for you & us if you chose to raise them in the QML and Qt Quick forum, rather than this General and Desktop one which is intended for widget and non-QML Qt topics.



  • @JonB alright.I will remember that