Changing the model does not affect the ComboBox

Let's assume we have the following code:

import QtQuick 2.4
import QtQuick.Window 2.0
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1

Window {
    id: win
    width: 800
    height: 600

    ListModel {
        id: listModel
        ListElement { name: "element1" }
        ListElement { name: "element2" }
        ListElement { name: "element3" }
    }

    ColumnLayout {
        anchors.centerIn: parent
        width: 200
        height: 200
        ComboBox {
            model: listModel
            currentIndex: 1
            Layout.fillWidth: true
        }
        ListView {
            model: listModel
            delegate: Text {
                    text: name
                }
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Button {
            text: "Change model"
            onClicked: {
                listModel.get(1).name = "changed text";
                //listModel.setProperty(1,"name","changed text"); this line not works too
            }
        }
    }
}

      

Thus, pressing the button should change the model element with index 1. But changing the model only affects ListView

. ComboBox

remains unchanged. Why is this happening? Is this a bug or a feature? Is there a way to update ComboBox

after changing its model?

+3


source to share


1 answer


I had a similar problem, I used a workaround. In the onClicked button, create a copy of the model, modify it as you want, and then assign it to the ListViews model again:



ListView {
  id: listView
  ...
}

Button {
  onClicked: {
    var copy = listView.model;
    copy.get(1).name = "changed text";
    listView.model = copy;            }
  }
}

      

0


source







All Articles