简体   繁体   English

ComboBox QML 选择后不显示项目文本

[英]ComboBox QML does not show item text after selection

I have a QML ComboBox whose model is defined as a C++ QList < QObject* >.我有一个 QML ComboBox,其模型定义为 C++ QList < QObject* >。 When I open the drop down list, I can see all items defined in the C++ model, but after selection, the selected item is not shown.当我打开下拉列表时,可以看到C++模型中定义的所有项,但是选择后,未显示所选项。 So, items are only visible in the drop down element.因此,项目仅在下拉元素中可见。 The relevant part of the qml file is: qml文件的相关部分是:

ComboBox {
    id: placesCombo
    anchors.top: parent.top
    width: parent.width
    model: myModel
    delegate: ItemDelegate {
        width: placesCombo.width
        contentItem: Text {
            id: placesComboItem
            text: displayLabel
            elide: Text.ElideRight
            verticalAlignment: Text.AlignVCenter
        }
    }
}

How to display in the closed combobox the item text previously selected in the drop down element?如何在关闭的组合框中显示先前在下拉元素中选择的项目文本?

According to the docs :根据文档

textRole : string文本角色:字符串

This property holds the model role used for populating the combo box.此属性包含用于填充组合框的模型角色。

When the model has multiple roles, textRole can be set to determine which role should be displayed.当模型有多个角色时,可以设置 textRole 来确定应该显示哪个角色。

You have to indicate the role of the model to be displayed through textRole.您必须通过 textRole 指示要显示的模型的角色。

ComboBox {
    id: placesCombo
    textRole: "displayLabel"
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM