“Typecast” XmlListModel to Listmodel

2019-06-06 09:26发布

I have a XmlListModel that fetches data from a webserver, which works great.

However, I want to use that in a CLComboBox (from the Colibri library).

If I set the parameter ListModel to my XmlListModel I get:

Non-existent attached object 
         ListModel:xmlModel13

But it does exists; since a ListView-object can use the very same model.

It seems to me that the CLComboBox only accepts a ListModel, so is there any way to transform a XmlListModel to a ListModel easily?

标签: qml
2条回答
仙女界的扛把子
2楼-- · 2019-06-06 09:54

You can populate the ListModel inside the CLComboBox using a loop.

查看更多
▲ chillily
3楼-- · 2019-06-06 10:19

You cannot cast QDeclarativeXmlListModel to QDeclarativeListModel because they both direct descendants of QListModelInterface.

In my opinion Colibri should use a QObject (variant property) instead of QDeclarativeListModel. And listen for standard item-based-model signals e.g. I believe it could be fixed with some effort.

If you not willing to contribute to Colibri, I think @sabbour's answer is best option. Consider simple example:

XmlListModel {
    id: xmlModel
    source: "http://forumcinemas.lv/rus/xml/Events/"
    query: "/Events/Event"
    XmlRole { name: "label"; query: "Title/string()" }
    XmlRole { name: "value"; query: "ID/number()"; isKey: true }
    onStatusChanged: {
        if (status == XmlListModel.Ready) {
            for (var i=0; i<count; i++) {
                var item = get(i)
                list_model.append({label: item.label,
                                   value: item.value,
                                   selected: false})
            }
            // CLComboBox doesnt seem to listen for model updates
            combo_box.items = list_model
        }
    }
}

ListModel {
    id: list_model
}

CLComboBox {
    id: combo_box
    // ...
}
查看更多
登录 后发表回答