当前位置:网站首页>Rosen's QT journey 102 listmodel
Rosen's QT journey 102 listmodel
2022-07-28 16:24:00 【Allen Roson】
1.ListModel summary
ListModel It's a simple container , Can contain ListElement Type stores data .
ListModel The number of data items can be used count Attribute gets . To maintain the data in the model , This type also provides a series of functions , Including additions append(), Insert people insert()、 Move move()、 remove remove() And replace set() etc. . These functions all accept dictionary types as their arguments , This dictionary type will be automatically converted into ListElement object . If you need to modify the model ListElement The content in , have access to setProperty() function , This function can modify the given index position ListElement The attribute value .
ListElement Need to be in ListModel In the definition of , Indicates that you can ListView or Repeater Data items used in
ListElement The use of is the same as others QML There is basically no difference in type , The difference is ,ListElement There is no fixed attribute , But a series of customized key values . You can put ListElement As a set of key value pairs , The key is called role( role ). It uses the same syntax as attributes to define . Roles define how to access data , It also defines the data itself . The character's name begins with a lowercase letter , And it should be all in the given model ListElement Common name . The value of the role must be a simple constant : character string ( With quotation marks , Can be included in QT_ TR_NOOP in call )、 Boolean type (true and false)、 Numbers or enumerations are similar ( for example AlignText. AlignHCenter). The name of the role is used by delegates to obtain data . The name of each role can be accessed within the scope of the delegate , And point to the current ListElement Corresponding value in . in addition , Roles can also contain list data , For example, it contains multiple ListElement.
2. Code example
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQml.Models 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel {
id: fruitModel
ListElement {
name: "Apple"
cost: 2.45
attributes: [
ListElement {
description: "core"
},
ListElement {
description: "Deciduous"
}
]
}
ListElement {
name: "Orange"
cost: 3.25
attributes: [
ListElement {
description: "Citrus"
}
]
}
ListElement {
name: "Banana"
cost: 1.95
attributes: [
ListElement {
description: "Tropical"
},
ListElement {
description: "Seedless"
}
]
}
}
Component {
id: fruitDelegate
Item {
width: 200
height: 50
Text {
id: nameField
text: name
}
Text {
text: '$' + cost
anchors.left: nameField.right
}
Row {
anchors.top: nameField.bottom
spacing: 5
Text {
text: "Attributes:"
}
Repeater {
model: attributes
Text {
text: description
}
}
}
MouseArea {
anchors.fill: parent
onClicked: fruitModel.setProperty(index, "cost", cost * 2)
}
}
}
ListView {
anchors.fill: parent
model: fruitModel
delegate: fruitDelegate
}
}
Running results :

3. Code interpretation
The above code uses a ListModel Model object , A list for storing fruit information .ListModel Contains 3 Data entries , By one ListElement Type said . Every ListElement There are 3 A character ;name, cost and attributes, They represent the names of fruits 、 Selling price and feature description , among attributes The role uses list data . It's used here ListView Show this model ( You can also use Repeater, The method is similar ).ListView You need to specify two properties :model and delegate.model Property specifies the defined fruitModel Model ;delegate Specify custom delegates . Use here Component Inline components act as delegates , It uses Text、Row etc. Qt Quick The project defines how each data entry is displayed . It can be used directly ListElement Roles defined in . about attributes role , It's used here Repeater Display . Delegates also use MouseArea, Called in it setProperty() function . When you click on a data entry , Its price will double . It's used here index Get the index of data items clicked by the mouse in the model .
Be careful : Once the dynamically created content is set, it can no longer be modified .setProperty Functions can only modify the data of elements that are explicitly defined directly in the model .
边栏推荐
- R语言使用GGally包的ggpairs函数可视化分组多变量的两两关系图、设置alpha参数改变图像透明度、对角线上连续变量密度图、离散变量条形图、两两关系图中包含散点图、直方图、箱图以及相关性数值
- leetcode 题目
- Telecommuting can be easily realized in only three steps
- Basic structure and operation principle of solar street lamp
- LwIP development | socket | TCP | client
- 2021 Kent interview question 1
- 8051 series MCU firmware upgrade IAP
- Roson的Qt之旅#102 ListModel
- One channel encoder, two channels Di speed measurement, RS485 serial port connected to one channel do alarm module ibf151
- Implementation of skip table
猜你喜欢

LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)

Telecommuting can be easily realized in only three steps

Dynamic programming -- digital statistics DP

动态规划 --- 数位统计DP

js 数组(总结)

Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation

2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161

2021 肯特面试题2

QT打包

8051 series MCU firmware upgrade IAP
随机推荐
Paging query in applet
Fifth uncle's thinking
JS linked list 01
High speed counter to rs485modbus RTU module ibf150
魏建军骑宝马也追不上李书福
五舅的思考
Play dead prototype chain
Is MySQL query limit 1000,10 as fast as limit 10? If I want to page, what should I do?
Darknet training yolov4 record
LwIP development | socket | TCP | client
5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服
R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
Instructions for mictr01 Tester development kit (vibrating wire acquisition reader)
远距离串口服务器( 适配器)UART/I2C/1-Wire/SPI PS304常见问题及注意事项
2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
High precision absolute angle sensor application high speed angle monitoring
Remember the common JS methods of projects
Automatic conversion and cast
深部位移监测系统wk813应用边坡、大坝、堤防、铁路和建筑基坑开挖等深部位移测量
js 栈