使用QML创建ComboBox的示例代码
以下是一个使用QML创建ComboBox的示例代码,让我们详细了解一下这个过程。
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 400
height: 300
title: "QML ComboBox示例"
ComboBox {
id: comboBox
width: 200
model: ListModel {
ListElement { text: "选项1" }
ListElement { text: "选项2" }
ListElement { text: "选项3" }
}
currentIndex: 0
placeholderText: "请选择一个选项"
onCurrentIndexChanged: {
var selectedText = comboBox.currentText;
console.log("当前选择的选项是:" + selectedText);
}
}
}
在这个示例中,我们创建了一个QML应用程序窗口(ApplicationWindow),并在其中添加了一个ComboBox组件。ComboBox是一个下拉列表框,可以显示多个选项供