0

我做了非常简单的 QAbstractListModel 例子。这个例子也在这个链接上展示:http: //doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html。一个程序结果是这样的:

程序结果 我在 view.qml 上做了一点改变,像这样

ComboBox {
id:mycombo
width: 200; height: 250

model: myModel
textrole:"type"
onCurrentTextChanged: {

   console.log(mycombo.model.get(currentIndex).size);
}
}

在这里,Combobox 显示动物类型。但我想在 onCurrentTextChanged 事件时写入“大小”值。但我无法获得类型值。

4

1 回答 1

0

我认为您的 cpp 模型实施失败了。因为当我使用 qml 模型时,它可以正常工作:

ListModel {
    id: myModel
    ListElement{
        type: "mouse"
        size: "small"
    }
    ListElement{
        type: "cat"
        size:"medium"

    }
    ListElement{
        type: "elephant"
        size:"large"
    }
}

ComboBox {
    id:mycombo
    width: 200; height: 250

    model: myModel
    textRole:"type"

    onCurrentTextChanged: {
       console.log(mycombo.model.get(currentIndex).size);
    }
}

输出:

qml: small
qml: medium
qml: large
于 2016-01-16T22:35:23.667 回答