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 6 Aug 2020, 13:55 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
                }
            }
        }
    }
    
    O 1 Reply Last reply 6 Aug 2020, 15:16
    0
    • C chilarai
      6 Aug 2020, 13:55

      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
                  }
              }
          }
      }
      
      O Offline
      O Offline
      ODБOï
      wrote on 6 Aug 2020, 15:16 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 6 Aug 2020, 15:23
      2
      • O ODБOï
        6 Aug 2020, 15:16

        @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 6 Aug 2020, 15:23 last edited by
        #3

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

        JonBJ 1 Reply Last reply 6 Aug 2020, 15:45
        1
        • C chilarai
          6 Aug 2020, 15:23

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 6 Aug 2020, 15:45 last edited by JonB 8 Jun 2020, 15:46
          #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 6 Aug 2020, 15:55
          2
          • JonBJ JonB
            6 Aug 2020, 15:45

            @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 6 Aug 2020, 15:55 last edited by
            #5

            @JonB alright.I will remember that

            1 Reply Last reply
            0

            1/5

            6 Aug 2020, 13:55

            • Login

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