QML创建一个带多个下拉输入框的窗口(ComboBox)

来源:互联网 发布:电工电子技术网络课 编辑:程序博客网 时间:2024/06/07 17:17

在网上搜了很多资料,用QML怎么实现下拉框的方法很少,并且都很复杂,我按照他们的方法实现的下拉框效果并不是很好,在问了公司的老司机后,他告诉我一个很好用的QML类:ComboBox,下面直接看代码和效果图

import QtQuick 2.5import QtQuick.Window 2.2import QtQuick.Controls 1.4  //使用 Button 控件必须包含import QtQuick.Layouts 1.1  //使用 GridLayout 控件必须包含Window {    visible: true    width: 610    height: 75    title: qsTr("ComboBox")    GridLayout {  //使其内部的所有控件以表格形式排列        Repeater {  //复制控件,model 为复制的控件数            id: repeater            model: 3            Item {                width: 200                ComboBox {                    id:combox                    currentIndex: 2                    model: ListModel {                        id: cbItems                        ListElement { text: "Banana"; color: "Yellow" }                        ListElement { text: "Apple"; color: "Green" }                        ListElement { text: "Coconut"; color: "Brown" }                    }                    width: 200                    onCurrentIndexChanged: {                        console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)                    }                }                Layout.row: 1  //控件所在的行号                Layout.column: index  //控件所在的列号            }        }    }}


2 0