ComboBox填充数据模型,数据模型通常是JavaScript数组,ListModel或者是整数,但是也支持其他类型的数据模型。另外,ComboBox还可以编辑

    属性

    acceptableInput : bool,此属性控制组合框是否包含可编辑文本字段中的可接受文本

    count : int,组合框中的项目数

    currentIndex : int,保存组合框中当前项的索引,只读

    currentText : string,组合框中当前项的文本,只读

    delegate : Component,包含一个委托,该委托在组合框弹出窗口中显示项目

    displayText : string,保存组合框按钮上显示的文本

    down : bool,保存组合框按钮是否在视觉上向下

    editText : string,指示将文本保存在可编辑组合框的文本字段中

    editable : bool,控制组合框是否可编辑

    flat : bool,控制组合框按钮是否平坦

    highlightedIndex : int,表示组合框弹出列表中突出显示项的索引,,只读

    indicator : Item,包含拖放指示器项

    inputMethodComposing : bool,表示可编辑组合框中是否具有部分文本输入采用某种输入方法

    inputMethodHints : flags,为输入法提供有关组合框的预期内容及其操作方式的提示

    model : model,控制着为组合框提供数据的模型

    popup : Popup,包含弹出窗口

    pressed : bool,代表组合框按钮是否以物理的方式按下

    textRole : string,表示用于填充组合框的模型角色

    validator : Validator,包含可编辑组合框的输入文本验证程序

    方法

    void decrementCurrentIndex()

    如果弹出列表可见,则递减组合框的当前索引或突出显示的索引

    int find(string text, flags)

    返回指定文本的索引,如果未找到匹配项,则返回 -1

    void incrementCurrentIndex()

    如果弹出列表可见,则增加组合框的当前索引或突出显示的索引

    void selectAll()

    选择组合框的可编辑文本字段中的所有文本

    string textAt(int index)

    返回指定索引的文本,如果索引超出范围,则返回空字符串

    信号

    void accepted()

    在可编辑的组合框上按下 Return 或 Enter 键时会发出此信号。如果输入的字符串不在模型中,则将currentIndex 设置为 -1,并且 currentText 将相应地更新。注意:如果组合框上设置了 validator,则只有在输入处于 acceptable 状态时才会发出信号。

    void activated(int index)

    用户在弹出窗口进行选择项目激活索引处的项目时,将发出此信号。

    void highlighted(int index)

    当弹出列表中索引处的项目被用户突出显示时,将发出此信号

    数字

    1. ComboBox{
    2. anchors.centerIn: parent
    3. model:[1,2,3,4,5,6,7,8,9]
    4. onActivated: {
    5. console.log(displayText)
    6. }
    7. }

    填充ListModel的ComboBox

    1. ComboBox {
    2. editable: true
    3. anchors.centerIn: parent
    4. model: ListModel {
    5. id: num
    6. ListElement { text: "One" }
    7. ListElement { text: "Two" }
    8. ListElement { text: "Three" }
    9. }
    10. onAccepted: {
    11. if (find(editText) === -1)
    12. num.append({text: editText})
    13. }
    14. }
    15. }

    key => value

    1. ComboBox {
    2. textRole: "key"
    3. model: ListModel {
    4. ListElement { key: "One"; value: 123 }
    5. ListElement { key: "Two"; value: 456 }
    6. ListElement { key: "Three"; value: 789 }
    7. }
    8. }

    自定义

    1. ComboBox {
    2. anchors.centerIn: parent
    3. id: control
    4. model: ["One", "Two", "Three"]
    5. delegate: ItemDelegate { //呈现标准视图项 以在各种控件和控件中用作委托
    6. width: control.width
    7. contentItem: Text {
    8. text: modelData //即model中的数据
    9. color: "green"
    10. font: control.font
    11. verticalAlignment: Text.AlignVCenter
    12. }
    13. }
    14. contentItem: Text { //界面上显示出来的文字
    15. leftPadding: 5 //左部填充为5
    16. text: control.displayText //表示ComboBox上显示的文本
    17. font: control.font //文字大小
    18. color: control.pressed ? "orange" : "black" //文字颜色
    19. verticalAlignment: Text.AlignVCenter //文字位置
    20. }
    21. background: Rectangle { //背景项
    22. implicitWidth: 120
    23. implicitHeight: 40
    24. color: "green"
    25. border.width: 1
    26. radius: 2
    27. }
    28. popup: Popup { //弹出项
    29. y: control.height
    30. width: control.width
    31. implicitHeight: contentItem.implicitHeight
    32. padding: 1
    33. //istView具有一个模型和一个委托。模型model定义了要显示的数据
    34. contentItem: ListView { //显示通过ListModel创建的模型中的数据
    35. clip: true
    36. implicitHeight: contentHeight
    37. model: control.popup.visible ? control.delegateModel : null
    38. }
    39. background: Rectangle {
    40. border.color: "green"
    41. radius: 2
    42. }
    43. }
    44. }

    如何设置默认值

    displayText: editobject.type