前面的章节我 们介绍过模型视图。这是一种数据和显示相分离的技术,在 Qt 中有着非常重要的地位。在 QtQuick 中,数据和显示的分离同样也是利用这种“模型-视图”技术实现的。对于每一个视图,数据元素的可视化显示交给代理完成。与 Qt/C++ 类似,QtQuick 提供了一系列预定义的模型和视图。本章开始,我们着重介绍这部分内容。这部分内容主要来自http://qmlbook.org/ch06/index.html,在此表示感谢。

由于 QtQuick 中的模型视图的基本概念同前面的章节没有本质的区别,所以这里不再赘述这部分内容。

将数据从表现层分离的最基本方法是使用Repeater元素。Repeater元素可以用于显示一个数组的数据,并且可以很方便地在用户界面进行定位。Repeater的模型范围很广:从一个整型到网络数据,均可作为其数据模型。

Repeater最简单的用法是将一个整数作为其model属性的值。这个整型代表Repeater所使用的模型中的数据个数。例如下面的代码中,model: 10代表Repeater的模型有 10 个数据项。

import QtQuick 2.2

Column {
spacing: 2
Repeater {
model: 10
Rectangle {
width: 100
height: 20
radius: 3
color: "lightBlue"
Text {
anchors.centerIn: parent
text: index
}
}
}
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import QtQuick 2.2   Column {     spacing: 2     Repeater {         model: 10         Rectangle {             width: 100             height: 20             radius: 3             color: "lightBlue"             Text {                 anchors.centerIn: parent                 text: index             }         }     } }

现在我们设置了 10 个数据项,然后定义一个Rectangle进行显示。每一个Rectangle的宽度和高度分别为 100px 和 20px,并且有圆角和浅蓝色背景。Rectangle中有一个Text元素为其子元素,Text文本值为当前项的索引。代码运行结果如下:

中国学网 www.xue163.com

虽然指定模型项的个数很简单,但实际用处不大。Repeater还支持更复杂的方式,例如,把一个 JavaScript 数组作为模型。JavaScript 数组元素可以是任意类型:字符串、数字或对象。在下面的例子中,我们将一个字符串数组作为Repeater的模型。我们当然可以使用index获得当前索引,同时,我们也可以使用modelData访问到数组中的每一个元素的值:

import QtQuick 2.2

Column {
spacing: 2
Repeater {
model: ["Enterprise", "Colombia", "Challenger", "Discovery", "Endeavour", "Atlantis"]
Rectangle {
width: 100
height: 20
radius: 3
color: "lightBlue"
Text {
anchors.centerIn: parent
text: index +": "+modelData
}
}
}
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import QtQuick 2.2   Column {     spacing: 2     Repeater {         model: ["Enterprise", "Colombia", "Challenger", "Discovery", "Endeavour", "Atlantis"]         Rectangle {             width: 100             height: 20             radius: 3             color: "lightBlue"             Text {                 anchors.centerIn: parent                 text: index +": "+modelData             }         }     } }

代码运行结果如下:

中国学网 www.xue163.com

由于能够使用 JavaScript 数组作为Repeater的模型,而 JavaScript 数组能够以对象作为其元素类型,因而Repeater就可以处理复杂的数据项,比如带有属性的对象。这种情况其实更为常见。相比普通的 JavaScript 对象,更常用的是ListElement类型。类似普通 JavaScript 对象,每一个ListElement可以有任意属性。例如下面的代码示例中,每一个数据项都有一个名字和外观颜色。

import QtQuick 2.2

Column {
spacing: 2
Repeater {
model: ListModel {
ListElement { name: "Mercury"; surfaceColor: "gray" }
ListElement { name: "Venus"; surfaceColor: "yellow" }
ListElement { name: "Earth"; surfaceColor: "blue" }
ListElement { name: "Mars"; surfaceColor: "orange" }
ListElement { name: "Jupiter"; surfaceColor: "orange" }
ListElement { name: "Saturn"; surfaceColor: "yellow" }
ListElement { name: "Uranus"; surfaceColor: "lightBlue" }
ListElement { name: "Neptune"; surfaceColor: "lightBlue" }
}

Rectangle {
width: 100
height: 20
radius: 3
color: "lightBlue"
Text {
anchors.centerIn: parent
text: name
}

Rectangle {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
anchors.leftMargin: 2

width: 16
height: 16
radius: 8
border.color: "black"
border.width: 1

color: surfaceColor
}
}
}
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 import QtQuick 2.2   Column {     spacing: 2     Repeater {         model: ListModel {             ListElement { name: "Mercury"; surfaceColor: "gray" }             ListElement { name: "Venus"; surfaceColor: "yellow" }             ListElement { name: "Earth"; surfaceColor: "blue" }             ListElement { name: "Mars"; surfaceColor: "orange" }             ListElement { name: "Jupiter"; surfaceColor: "orange" }             ListElement { name: "Saturn"; surfaceColor: "yellow" }             ListElement { name: "Uranus"; surfaceColor: "lightBlue" }             ListElement { name: "Neptune"; surfaceColor: "lightBlue" }         }           Rectangle {             width: 100             height: 20             radius: 3             color: "lightBlue"             Text {                 anchors.centerIn: parent                 text: name             }               Rectangle {                 anchors.left: parent.left                 anchors.verticalCenter: parent.verticalCenter                 anchors.leftMargin: 2                   width: 16                 height: 16                 radius: 8                 border.color: "black"                 border.width: 1                   color: surfaceColor             }         }     } }

运行结果如下图所示:

中国学网 www.xue163.com

ListElement的每个属性都被Repeater绑定到实例化的显示项。正如上面代码中显示的那样,这意味着每一个用于显示数据的Rectangle作用域内都可以访问到ListElementnamesurfaceColor属性。

像上面几段代码中,Repeater的每一个数据项都使用一个Rectangle渲染。事实上,这是由于Repeater具有一个delegate的默认属性,由于Rectangle没有显式赋值给任何一个属性,因此它直接成为默认属性delegate的值,所以才会使用Rectangle渲染。理解了这一点,我们就可以写出具有显式赋值的代码:

import QtQuick 2.2

Column {
spacing: 2
Repeater {
model: 10
delegate: Rectangle {
width: 100
height: 20
radius: 3
color: "lightBlue"
Text {
anchors.centerIn: parent
text: index
}
}
}
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import QtQuick 2.2   Column {     spacing: 2     Repeater {         model: 10         delegate: Rectangle {             width: 100             height: 20             radius: 3             color: "lightBlue"             Text {                 anchors.centerIn: parent                 text: index             }         }     } }

实际上,这段代码与前面提到的是等价的。

Qt 学习之路 2(84):Repeater的更多相关文章

  1. Qt 学习之路 2(51):布尔表达式树模型

    Qt 学习之路 2(51):布尔表达式树模型 豆子 2013年5月15日 Qt 学习之路 2 17条评论 本章将会是自定义模型的最后一部分.原本打算结束这部分内容,不过实在不忍心放弃这个示例.来自于 ...

  2. 《Qt 学习之路 2》目录

    <Qt 学习之路 2>目录 <Qt 学习之路 2>目录  豆子  2012年8月23日  Qt 学习之路 2  177条评论 <Qt 学习之路 2>目录 序 Qt ...

  3. QT学习之路--创建一个对话框

    Q_OBJECT:这是一个宏,凡是定义信号槽的类都必须声明这个宏. 函数tr()全名是QObject::tr(),被他处理过的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用. 对于QT学 ...

  4. 转载: Qt 学习之路 2归档

    Qt 学习之路 2归档 http://www.devbean.net/2012/08/qt-study-road-2-catelog/

  5. Qt学习之路

      Qt学习之路_14(简易音乐播放器)   Qt学习之路_13(简易俄罗斯方块)   Qt学习之路_12(简易数据管理系统)   Qt学习之路_11(简易多文档编辑器)   Qt学习之路_10(Qt ...

  6. Qt 学习之路 2

    Qt 学习之路 2 | DevBean Tech World Qt 学习之路 2 Qt 学习之路 2 目录

  7. Qt 学习之路 2(76):QML 和 QtQuick 2

    Home / Qt 学习之路 2 / Qt 学习之路 2(76):QML 和 QtQuick 2 Qt 学习之路 2(76):QML 和 QtQuick 2  豆子  2013年12月18日  Qt ...

  8. Qt 学习之路 2(74):线程和 QObject

    Home / Qt 学习之路 2 / Qt 学习之路 2(74):线程和 QObject Qt 学习之路 2(74):线程和 QObject  豆子  2013年12月3日  Qt 学习之路 2  2 ...

  9. Qt 学习之路 2(73):Qt 线程相关类

    Home / Qt 学习之路 2 / Qt 学习之路 2(73):Qt 线程相关类 Qt 学习之路 2(73):Qt 线程相关类  豆子  2013年11月26日  Qt 学习之路 2  7条评论 希 ...

  10. Qt 学习之路 2(72):线程和事件循环

    Qt 学习之路 2(72):线程和事件循环 <理解不清晰,不透彻>  --  有需求的话还需要进行专题学习  豆子  2013年11月24日  Qt 学习之路 2  34条评论 前面一章我 ...

随机推荐

  1. Redis的windows安装

    第一步:下载Windows下的Redis链接:http://web.kuaipan.cn/n/drive/files/179275650081763322 第二不:打开cmd窗口命令:定位的Redis ...

  2. filter过滤action的问题

    今天犯了一个错误,结果白白浪费了半个下午的时间,特记于此. filter过滤Action的时候,要把过滤器配置在Struts2拦截器的前面,这样过滤器才能过滤到Action,否则不可以.

  3. python中的pip安装

    windows下安装PIP 当前环境(windows 7,python安装路径为c:\Python) 1.首先到官网下载(https://pypi.python.org/pypi/setuptools ...

  4. bashrc - PS1(提示符配色)

    PS1设置: liBlack="\[\033[0;30m\]"boBlack="\[\033[1;30m\]"liRed="\[\033[0;31m\ ...

  5. Sass与Compress实战:第三章

    概要:这一章将介绍Compass如何使Web设计中最基础的部分——布局变得简单. 本章内容: ● 网格布局的基本原理以及何时使用网格布局 ● 使用Compass时的CSS网格布局框架选项 ● 使用排版 ...

  6. php基础(三)超全局变量

    超全局变量 在 PHP 4.1.0 中引入,是在全部作用域中始终可用的内置变量. PHP 全局变量 - 超全局变量 PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可 ...

  7. Ubuntu shortcuts

    Ubuntu shortcuts 打开系统管理器 gnome-system-monitor

  8. delphi学习treeview中从表列名和数据添加为目录并双击自动选中

    1 unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syst ...

  9. mysql字段更改操作命令

    1) 加索引   mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]); 例子: mysql> alter table employee ...

  10. LeetCode OJ 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...