Changing model doesn't affect ComboBox

1.8k views Asked by At

Suppose, 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
            }
        }
    }
}

So clicking the button have to change model's element with index 1. But changing the model affects only ListView. The ComboBox remains unchanged. Why that happens? Is it bug or feature? Is there a way to update ComboBox after changing its model?

1

There are 1 answers

1
MarPan On

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

ListView {
  id: listView
  ...
}

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