当前位置:网站首页>Roson的Qt之旅#107 QML ListView
Roson的Qt之旅#107 QML ListView
2022-08-08 22:10:00 【Allen Roson】
1.概述
ListView显示的数据来自于从内置的QML类型创建的模型,如ListModel和XmlListModel,或在C++中定义的继承于QAbstractItemModel或QAbstractListModel的自定义模型类。
ListView有一个模型,它定义了要显示的数据,还有一个委托,它定义了数据应该如何显示。列表视图中的项目是水平或垂直布置的。由于ListView继承了Flickable,所以ListView本质上是可弹出的。
2.使用实例
下面的例子显示了一个简单的列表模型的定义,它被定义在一个叫做ContactModel.qml的文件中。
import QtQuick 2.0
ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
另一个组件可以在ListView中显示这个模型数据,像这样。
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListView {
width: 180; height: 200
model: ContactModel {}
delegate: Text {
text: name + ": " + number
}
}
}
在这里,ListView为其模型创建了一个ContactModel组件,为其委托创建了一个Text项目。视图将为模型中的每个项目创建一个新的Text组件。委托人能够直接访问模型的名字和号码数据。
运行效果如下:
下面是一个改进的列表视图。委托在视觉上得到了改进,并被移到一个单独的contactDelegate组件中。
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
width: 180; height: 200
Component {
id: contactDelegate
Item {
width: 180; height: 40
Column {
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
}
}
ListView {
anchors.fill: parent
model: ContactModel {}
delegate: contactDelegate
highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
focus: true
}
}
}
运行效果如下:
上面的代码使用了高亮属性highlight,当前选择的项目被蓝色的矩形高亮显示,焦点被设置为 "true",以启用列表视图的键盘导航。列表视图本身是一个焦点范围(更多细节见Qt Quick中的键盘焦点)。
代理根据需要被实例化,并且可以在任何时候被销毁。它们是ListView的contentItem的父对象,而不是视图本身。状态不应该被存储在一个委托中。
ListView将一些属性附加到委托的根项上,例如ListView.isCurrentItem。在下面的例子中,根委托项可以作为ListView.isCurrentItem直接访问这个附加属性,而子contactInfo对象必须作为wrapper.ListView.isCurrentItem引用这个属性。
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListView {
width: 180; height: 200
Component {
id: contactsDelegate
Rectangle {
id: wrapper
width: 180
height: contactInfo.height
color: ListView.isCurrentItem ? "black" : "red"
Text {
id: contactInfo
text: name + ": " + number
color: wrapper.ListView.isCurrentItem ? "red" : "black"
}
}
}
model: ContactModel {}
delegate: contactsDelegate
focus: true
}
}
运行效果如下:
注意事项:
视图不会自动启用剪辑。如果视图没有被其他项目或屏幕剪掉,就有必要设置clip: true以使视图之外的项目被很好地剪掉。
边栏推荐
- 爬虫系列:读取 CSV、PDF、Word 文档
- Crawler series: read CSV, PDF, Word documents
- SRv6故障管理
- Zero Digital Reports Digital Financial Innovation to the Secretary of Hainan Provincial Party Committee
- 嵌入式开发:提示和技巧——C 语言中要避免的8个保留字
- Mysql汉字乱码的问题
- 国产GPU大厂景嘉微半年净利润1.25亿元 旗下产品大卖
- CrossFormer: A Versatile Vision Transformer -based on Cross - Scale Transformer paper and code parsing
- sqli_libsLess-2 GET - Error based - Intiger based (基于错误的GET整型注入)
- Crawler Series: Storing CSV Files
猜你喜欢
随机推荐
Matlab的下载
反向代理服务器能干什么?
Cesium快速上手3-Billboard/Label/PointPrimitives图元使用讲解
编程需要无畏感
玩转C#网页抓取
How do I use cURL with a proxy?
CrossFormer: A Versatile Vision Transformer -based on Cross - Scale Transformer paper and code parsing
百度 IP 查询
JQGrid通过json请求nodejs数据,表格信息保存在mysql数据库中
Hands-on Deep Learning_Transposed Convolution
C盘的空间管理
零数科技“链上海南”——深耕数字金融领域
How is the commission for online account opening reduced?Is it safe to open an account with an online account manager?
软件设计原则
Chrome代理管理器插件
BSV 中的零开销私人时间戳
2020上海智慧城市合作大会,零数科技受邀出席并入选优秀应用案例
峰会•沙龙•招聘 | 记零数科技多线并进的一天
2020-03-09
Matlab to download