repeater是用来重复创建同一类型组件的对象,嗯。。提到这个首先得知道QML的一些机制,提倡设计者让数据与模型分离开来,数据是数据模型是模型。repeater就可以根据数据,调用同一个模型去创建多个对象。直接举个例子吧

 import QtQuick 2.0Row {Repeater {model: 3delegate:Rectangle {width: 100; height: 40border.width: 1color: "yellow"}}}

这是QT帮助文档里面的代码 Row是一个布局器,只看repeater部分,这个是代表创建3个这样的rectangle。

model属性存放数据,可以是数字,也可以是数组,还可以是一个数据类型的对象

delegate属性存放模型,在repeater中,这个delegate属性名是可以省略的,直接写模型也是没问题的,这个模型会调用model的数据

通过repeater生成的对象如果想要访问的话就需要通过repeater的 Item itemAt(index) 函数进行访问

定义模型就不说了,这里具体讲的是数据的定义,一般数据分三种情况:

1.模型不需要数据,只需要指定个数

这种情况只需要将model给定一个数字,代表创建几个,就类似于开头例子那样

2.模型需要简单的数据,这个时候可以使用数组进行创建

这个时候数组的长度,就是创建的个数,数组的内容就是模型的数据,如下面的例子所示:

Repeater{id:id_repeatermodel: ["data1","data2"]Rectangle{width: 120height: 120color: "red"visible: index === 0Text {text: modelDataanchors.verticalCenter: parent.verticalCenteranchors.horizontalCenter: parent.horizontalCentercolor: "black"}}}

这样就会创建两个内部有文字的rectangle,并且只有第一个字符为“data1”的才会显示,这里访问数据就用modelData去访问数据,如果想要特殊指定,比如第二个要隐藏,就可以在模型内部通过判断index去修改(index代表是这个repeater的第几个对象)

3.如果模型需要比较复杂的数据,单纯的数组就不能满足条件了,这个时候就需要用到数据对象了,比较常用的是listmodel,基本上listmodel能满足绝大部分要求了,至于listmodel怎么用可以看看帮助文档,下面是这类型的示例

ListModel{id:id_modelListElement{mName:"data1"mScrp:"test1"}ListElement{mName:"data2"mScrp:"test2"}}Repeater{id:id_repeatermodel: id_modelRectangle{width: 120height: 120color: "red"visible: index === 0Text {text: mNamecolor: "black"}Text {text: mScrpcolor: "black"}}}

这个时候只需要将模型里面需要用到数据的地方写成对应的数据的名字,就能够进行访问了。

以上是它的基本用法,不多,但是基本都有需要注意的地方,这里才算是最重要的部分,了解到了这些在实际使用的时候会有大用处

第一二种需要注意的就是如果model的数组或者数字发生改变,那么repeater会销毁所有已经创建的对象再重新生成。数字改变很好理解,但是数组改变就有点复杂了。

数组改变:

1.数组内部值的改变,比如通过下标修改数组的值,这个时候虽然数组的下标值改变了,但是对repeater来说没有任何影响,甚至模型里面的数据都不会改变。

Repeater{id:id_repeatermodel: id_root.mDataRectangle{width: 120height: 120color: index === 1 ? "transparent" : "red"visible: index === 0Text {text: modelDataanchors.verticalCenter: parent.verticalCenteranchors.horizontalCenter: parent.horizontalCentercolor: "black"}MouseArea{anchors.fill: parentonClicked: {console.log("clicked")id_root.mData[0] = "changed"console.log(mData)}}}}

这行代码执行时,数据是更改了,但是模型的显示还是以前的数据。

2.model如果是判断为更改了对象的操作,这个时候会将有值判定的过程,如果判定两个值相等那就不会重新构建,但是如果值不等,就会重新全部创建

property var mData: ["hello","world"]property var mData2: ["hello","world"]property var bFlag: trueRepeater{id:id_repeatermodel: id_root.bFlag ? id_root.mData : id_root.mData2Rectangle{width: 120height: 120color: index === 1 ? "transparent" : "red"Text {text: modelDataanchors.verticalCenter: parent.verticalCenteranchors.horizontalCenter: parent.horizontalCentercolor: "black"}MouseArea{anchors.fill: parentonClicked: {console.log("click")id_root.bFlag = !id_root.bFlag}}Component.onCompleted: {console.log("create comp" + index)}}}

qml: create comp0
qml: create comp1
qml: click
qml: click

除了最开始创建时打印,更改并不会重新创建

现在将另一个数组改个值

property var mData: ["hello","world"]property var mData2: ["hello","create"]property var bFlag: trueRepeater{id:id_repeatermodel: id_root.bFlag ? id_root.mData : id_root.mData2Rectangle{width: 120height: 120color: index === 1 ? "transparent" : "red"Text {text: modelDataanchors.verticalCenter: parent.verticalCenteranchors.horizontalCenter: parent.horizontalCentercolor: "black"}MouseArea{anchors.fill: parentonClicked: {console.log("click")id_root.bFlag = !id_root.bFlag}}Component.onCompleted: {console.log("create comp" + index)}}}

qml: create comp0
qml: create comp1
qml: click
qml: create comp0
qml: create comp1

可以发现每次点击都会重新创建对象

如果是model的对象,那么它的改变就非常直观了,因为每个一串数据都会被当作一个对象,可以单独管理每一个模型,所以,只要model的数据串改变了那模型显示的数据就会 更改,并且不会重新创建对象,下面是一个示例

ListModel{id:id_modelListElement{mName:"data1"mScrp:"test1"}ListElement{mName:"data2"mScrp:"test2"}}Repeater{id:id_repeatermodel: id_modelRectangle{width: 120height: 120color: "red"visible: index === 0Text {text: mNamecolor: "black"}Component.onCompleted: {console.log("create")}}}Timer {interval: 500; running: true; repeat: trueonTriggered: {console.log("update")id_model.setProperty(0,"mName",Math.random().toString())}}

这个时候的打印

qml: create
qml: create
qml: update
qml: update
qml: update
qml: update

发现只会更新但是并不会重新生成对象

QML---Repeater相关推荐

  1. QML Repeater

    QML Repeater 中继器 参考视频:https://www.bilibili.com/video/BV1Ay4y1W7xd 参考:https://doc.qt.io/qt-5/qml-qtqu ...

  2. Roson的Qt之旅 #114 QML Repeater(重复器)

    1.概述 Repeater类型用于创建大量的类似项目.像其他视图类型一样,Repeater有一个模型和一个委托:对于模型中的每个条目,委托被实例化在一个用模型中的数据播种的环境中.重复器项目通常被包围 ...

  3. ipad一代应用_通过下一代图像提高您的应用程序性能

    ipad一代应用 It is no secret today that an app performance is an important component in the user experie ...

  4. QML delegate中使用Repeater

    在QML界面设计例如ListView中,通过设计delegate来显示代理model中的数据,在delegate中使用属性绑定的方式将model中对象的值显示出来,但如果delegate中使用了Rep ...

  5. Qt QML 菜单/目录/工具栏的全面攻略(TabBar、MenuBar、ToolBar、Button定制、Listview、Repeater)

    Qt QML 菜单/目录/工具栏的全面攻略 1. TabBar的工具栏/目录 1.1 演示 1.2 关键控件 1.3 源码 2 MenuBar 菜单 2.1 演示 2.2 关键控件 2.3 源码 3 ...

  6. qml之Repeater

    简介 Repeater用于创建大量类似项.与其他视图类型一样,Repeater有一个模型和一个委托:对于模型中的每一项,委托都在一个上下文中实例化.Repeater通常用于在定位器类型(如Row或Co ...

  7. QML类型:Repeater

    一.描述 Repeater 类型用于创建大量相似的项目.是一种视图元素.与其他视图类型一样,Repeater 有一个模型和一个委托. Repeater 项通常包含在定位器类型中,例如 Row 或 Co ...

  8. Qml学习笔记-Repeater的基本使用

    程序运行截图如下: 代码如下: Window {visible: truewidth: 150height: 500title: qsTr("RepeaterDemo")Colum ...

  9. QML(02)——重复性组件Repeater

    实现效果 //UnsoundRow.qmlimport QtQuick 2.0Item {id: unsoundRowproperty string activeOption: "生芽粒&q ...

  10. qml基础学习 基础概念

    一.概括 学习qt已有2年多的时间,从qt4.7开始使用直到现在正在使用的qt5.6,基本都在windows机器上做开发.最近有意向看了下qt的qml部分,觉着还是挺不错的,毕竟可以做嵌入式移动端产品 ...

最新文章

  1. RESTful API 设计指南(转)
  2. ie浏览器修复_继IE之后,微软要彻底放弃它们了...
  3. 如何设计一门语言(十一)——删减语言的功能
  4. c语言不规则窗口,C语言不规则数组和指针
  5. [5] ADB 与应用交互
  6. 跨平台异步IO库 libuv 源代码接口详解
  7. CSS3 3D切割轮播图
  8. Python中ASCII码的数字和字符的转换
  9. FLASH BUILDER 清除图片缓存
  10. Angular 学习笔记——$interpolateProvide
  11. VS2017 Visual Assist X破解方法
  12. 2010年08期《程序员》配套源码及相关链接
  13. c语言程序设计徐立辉答案,C语言程序设计实验及习题指导
  14. mac 系统服务器地址ping不通,MAC ping IP 地址(ping通/ping不通)
  15. hashmap怎么保证线程安全的方式
  16. 三国志战略版:北定中原剧本个性加点指引
  17. 使用Adb shell dumpsys检测Android的Activity任务栈
  18. android手机用多久会卡机,你知道Android手机用久了会卡,而iPhone则不会是为什么吗?...
  19. mysql出现2058,连接MySQL报“Error No.2058 Plugin caching_sha2_password could not be loaded”错误的解决办法...
  20. 浪潮之巅第一章 — 帝国的余辉(ATT)

热门文章

  1. C语言基础——统计由键盘输入的一行字符中数字、字母与其他的个数
  2. HelloWorld之jetty运行
  3. 洛朗级数与泰勒展开的区别
  4. DataGrip csv等文件 快速建表
  5. django 导出数据到excel表 导出excel表到目标路径及客户端下载
  6. 电子秤称东西用计算机怎么算,怎样将电子天平或电子秤连接到PC并直接将重量值读取到Excel中...
  7. 3dmax如何拆分模型_3dmax模型怎么分割
  8. SpringBoot+Vue实现前后端分离的宠物医院管理系统
  9. 均匀分布的期望与方差计算公式
  10. handlebars.js_Handlebars.js入门指南