【问题标题】:Changing model doesn't affect ComboBox更改模型不会影响 ComboBox
【发布时间】:2015-06-11 00:01:39
【问题描述】:

假设,我们有以下代码:

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

因此单击按钮必须更改索引为 1 的模型元素。但更改模型仅影响 ListViewComboBox 保持不变。 为什么会这样?是错误还是功能? ComboBox 更改模型后有没有办法更新它?

【问题讨论】:

    标签: qml qtquick2


    【解决方案1】:

    我遇到了类似的问题,我使用了一种解决方法。在按钮的 onClicked 函数中,创建模型的副本,根据需要进行更改,然后再次将其分配给 ListViews 模型:

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

    【讨论】:

    • 好的,看来是bug所以我为这个问题创建了bug report
    猜你喜欢
    • 2012-01-18
    • 2018-12-16
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多