【QML】组合框 QComboBox

2019-05-23  本文已影响0人  4thirteen2one

import QtQuick.Controls 2.5

ComboBox 是按钮和弹出列表的组合。它提供了一种以占用最少屏幕空间量的方式向用户显示选项列表的方法。

ComboBox 填充了数据模型。数据模型通常是 JavaScript 数组,ListModel 或 整数,但也支持其他类型的数据模型。

ComboBox 可以编辑。可编辑的组合框根据模型中可用的内容自动完成其文本。

以下示例演示了通过对接受的信号作出反应,将内容附加到可编辑的组合框。

ComboBox {
    editable: true
    model: ListModel {
        id: model
        ListElement { text: "Banana" }
        ListElement { text: "Apple" }
        ListElement { text: "Coconut" }
    }
    onAccepted: {
        if (find(editText) === -1)
            model.append({text: editText})
    }
}

ComboBox 能够可视化提供 modelData 角色的标准数据模型:

当使用具有多个命名角色的模型时,为了显示其文本和代理实例,必须使用特定的 textRole 配置 ComboBox。

ComboBox {
    textRole: "key"
    model: ListModel {
        ListElement { key: "First"; value: 123 }
        ListElement { key: "Second"; value: 456 }
        ListElement { key: "Third"; value: 789 }
    }
}

注意:如果为 ComboBox 分配了一个具有多个命名角色的数据模型,但未定义 textRole,则ComboBox 无法将其可视化并抛出 ReferenceError: modelData is not defined

属性

信号

方法

上一篇 下一篇

猜你喜欢

热点阅读