Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamically add combo box without resetting the previous selection

Dynamically add combo box without resetting the previous selection

Scheduled Pinned Locked Moved Solved General and Desktop
qmlcomboboxlistmodel
5 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chilarai
    wrote on last edited by
    #1

    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
                }
            }
        }
    }
    
    ODБOïO 1 Reply Last reply
    0
    • C chilarai

      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
                  }
              }
          }
      }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @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})
              }
          }
      }
      
      C 1 Reply Last reply
      2
      • ODБOïO ODБOï

        @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})
                }
            }
        }
        
        C Offline
        C Offline
        chilarai
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        1
        • C chilarai

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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.

          C 1 Reply Last reply
          2
          • JonBJ JonB

            @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.

            C Offline
            C Offline
            chilarai
            wrote on last edited by
            #5

            @JonB alright.I will remember that

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved