1. ListView
1.1 简介
在 QML
中,ListView
属于 QtQuick.Controls
模块,它是用于创建可滚动列表的元素。QtQuick.Controls
是 QtQuick
框架的一部分,提供了一系列可重用的用户界面元素和控件,用于快速构建交互式和响应式的用户界面。ListView
是其中的一个重要组件,用于显示大量数据并支持滚动浏览。
1.2 示例
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 300
height: 400
ListModel {
id: myModel
ListElement { name: "Alice"; age: 30 }
ListElement { name: "Bob"; age: 25 }
ListElement { name: "Charlie"; age: 35 }
}
ListView {
anchors.fill: parent
model: myModel
spacing: 10
delegate: Rectangle {
width: 200
height: 50
color: ListView.isCurrentItem ? "lightblue" : "white"
Text {
text: model.name
anchors.centerIn: parent
}
}
}
}