当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Remote serial port server (adapter) UART to 1-wire application

js 双向链表 01

IFD-x 微型红外成像仪(模块)的温度测量和成像精度

12V pulse speed measurement to 24V level signal conversion transmitter

Two special functions (arrow function and method)

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

小程序中的分页查询

软件问题修复跟踪系统实战开发教程(上篇)

Roson的Qt之旅#101 Qt Quick中的模型和视图

解决电脑恶意广告弹窗的思路
随机推荐
李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
12V pulse speed measurement to 24V level signal conversion transmitter
Basic structure and operation principle of solar street lamp
The deep displacement monitoring system wk813 is used to measure the deep displacement of slopes, dams, embankments, railways and building foundation pit excavation
R语言ggplot2可视化绘制线图(line plot)、使用gghighlight包突出高亮线图中满足组合判断条件的线图(satisfies both condition A and B)
Darknet training yolov4 record
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
振弦采集模块测量振弦传感器的流程步骤?
Writing of factorial
mysql 查看事件状态语句和修改办法
带你来浅聊一下!单商户功能模块汇总
High precision absolute angle sensor application high speed angle monitoring
2021 Kent interview question 3
A tour of grp:05 - GRP server streaming service end stream
[Multisim Simulation] LM339 zero crossing circuit simulation
2-channel Di high-speed pulse counter, 1-channel encoder to Modbus TCP wired wireless module ibf161
Two special functions (arrow function and method)
2021 Kent interview question 2
Note: the value is rounded up to ten, hundred, thousand, ten thousand