qml_基本元素运用示例


  1. 1. 介绍QML是一种描述语言,主要是对界面效果等的一种描述,它可以结合javaScript来进行更复杂的效果及逻辑实现。比如做个游戏,实现一些更有趣的功能等

  2. 2. 简单的例子
  3. import Qt 4.7
  4. Rectangle {
  5.     width: 200
  6.     height: 200
  7.     color: "blue"
  8. }
  9. 代码是绘制一个蓝色的矩形,宽 200 高 200, import包含一个qt4.7的包
  10. 3.基本元素的介绍(自己翻译意思会有出入,敬请见谅)
  11.         基本可视化项
  12. Item 基本的项元素 在QML中所有可视化的向都继承他
  13. Rectangle 基本的可视化矩形元素
  14. Gradient 定义一个两种颜色的渐变过程
  15. GradientStop 定义个颜色,被Gradient使用
  16. Image 在场景中使用位图
  17. BorderImage(特殊的项) 定义一张图片并当做边界
  18. AnimatedImage 为播放动画存储一系列的帧
  19. Text 在场景中使用文本
  20. TextInput 显示可编辑为文本
  21. IntValidator int 验证器
  22. DoubleValidator double 验证器
  23. RegExpValidator 验证字符串正则表达式
  24. TextEdit 显示多行可编辑文本
  25. 基本的交互项
  26. MouseArea 鼠标句柄交互
  27. FocusScope 键盘焦点句柄
  28. Flickable 提供一种浏览整张图片的一部分的效果,具体看例子
  29. Flipable 提供一个平面,可以进行翻转看他的前面或后面,具体看例子
  30. 状态
  31. State 定义一个配置对象和属性的集合
  32. PropertyChanges 使用一个State描述属性的改变
  33. StateGroup 包含一个状态集合和状态变换
  34. ParentChange 重新定义父集,也就是换个父节点
  35. AnchorChanges 在一个状态中改变anchors
  36. 动画和变换
  37. Behavior 默认的属性变换动画
  38. SequentialAnimation 对定义的动画串行播放
  39. ParallelAnimation 对定义的动画并行播放
  40. PropertyAnimation 属性变换动画
  41. NumberAnimation 对实数类型属性进行的动画
  42. Vector3dAnimation 对QVector3d进行的属性
  43. ColorAnimation 颜色进行的变换动画
  44. RotationAnimation 对旋转进行的变换动画
  45. ParentAnimation 对父节点进行变换的动画,改变绑定的父节点
  46. AnchorAnimation 对anchor 进行改变的动画
  47. PauseAnimation 延迟处理
  48. SmoothedAnimation 允许属性平滑的过度
  49. SpringAnimation 一种加速的效果
  50. PropertyAction 允许在动画过程中对属性的直接改变
  51. ScriptAction 允许动画过程中调用脚本
  52. Transition 在状态变换中加入动作变化
  53. 工作中的数据
  54. Binding 在创建的时候绑定一些数据到一些属性
  55. ListModel 定义链表数据
  56. ListElement 定义ListModel的一个数据项
  57. VisualItemModel 包含可视化项(visual items)到一个view中,相当是一个容器
  58. VisualDataModel 包含一个model和一个delegate,model包含需要的数据,delegate设计显示的项的信息,具体的去看例子
  59. Package 他的目的是把VisualDataModel共享给多个view,具体还要学习
  60. XmlListModel 特殊的一个模式使用XPath表达式,使用xml来设置元素,参考例子
  61. XmlRole XmlListModel的一个特殊的角色
  62. 试图
  63. ListView 提供一个链表显示模型试图
  64. GridView 提供一个网格显示模型试图
  65. PathView 提供一个内容沿着路径来显示的模型
  66. Path 定义一个PathView使用的轨迹
  67. PathLine 定义一个线性的轨迹
  68. PathQuad 定义一个二次贝塞尔曲线的轨迹
  69. PathCubic 定义一个三次贝塞尔曲线的轨迹
  70. PathAttribute 允许绑定一个属性上,具体看例子
  71. PathPercent 修改item分配的轨迹 不是很明了其中的意思
  72. WebView 允许添加网页内容到一个canvas上
  73. 定位器
  74. Column 整理它的子列()
  75. Row 整理它的子行()
  76. Grid 设置它的子到一个网格上
  77. Flow 目的是不让他的子项重叠在一起
  78. 实用
  79. Connections 明确连接信号和信号句柄
  80. Component 封装QML items 想一个组件一样
  81. Timer 提供时间触发器
  82. QtObject 基本的元素只包含objectName属性
  83. Qt qml全局Qt object提供使用的枚举和函数
  84. WorkerScript 允许在QML使用线程
  85. Loader 控制载入item或组件
  86. Repeater 使用一个模型创建多个组件
  87. SystemPalette 为Qt palettes提供一个通道
  88. FontLoader 载入字体根据名字或URL
  89. LayoutItem 允许声明UI元素插入到qtGraphicsView 布局中
  90. 变换
  91. Scale 分派item 缩放行为
  92. Rotation 分派item 旋转行为
  93. Translate 分派item 移动行为


  94. 4.基本元素的使用例子
  95. 1. Item位置是0,0 宽高分别是200
  96. Item {
  97.     x: 0; y: 0;
  98.     width: 200; height: 200;
  99. }
  100. 2. Rectangle位置是:0,0宽高分别是200, 颜色是红色
  101. Rectangle {
  102.     x: 0; y: 0;
  103.     width: 200; height: 200;
  104.     color: "red"
  105. }
  106. 3. Gradient GradientStop 分别在总高度的0 颜色红色 总高度的1/3 黄色 总高度的1是绿色
  107. Rectangle {
  108.     width: 100; height: 100
  109.     gradient: Gradient {
  110.         GradientStop { position: 0.0; color: "red" }
  111.         GradientStop { position: 0.33; color: "yellow" }
  112.         GradientStop { position: 1.0; color: "green" }
  113.     }
  114. }
  115. 4. Image 设置一张图片
  116. Image {
  117.     source: "../Images/button1.png"
  118. }
  119. 5. BorderImage 他将一张图片分成9部分
  120. 当图片进行缩放的时候
  121. A.1 3 7 9 位置的都不会进行缩放
  122. B. 2 8将根据属性horzontalTileMode 进行缩放
  123. C.4 6 将根据属性verticalTileMode 进行缩放
  124. D.5 将根据属性horzontalTileMode 和 verticalTileMode 进行缩放
  125. BorderImage {
  126.     width: 180; height: 180
  127.     // 分割1~9块的4个点是根据border设置的坐标来实现的
  128.     // 本别是距离坐标 上边 右边 下边的距离
  129.     border { left: 30; top: 30; right: 30; bottom: 30 }
  130.     horizontalTileMode: BorderImage.Stretch
  131.     verticalTileMode: BorderImage.Stretch
  132.     source: "../Images/button1.png"
  133. }

  134. 6. AnimatedImage 主要用于播放gif图片
  135. Rectangle {
  136.     width: animation.width; height: animation.height + 8
  137.     AnimatedImage { id: animation; source: "animation.gif" }
  138.     Rectangle {
  139.         property int frames: animation.frameCount
  140.         width: 4; height: 8
  141.         x: (animation.width - width) * animation.currentFrame / frames
  142.         y: animation.height
  143.         color: "red"
  144.     }
  145. }
  146. 7. Text 显示文本(具体的其他设置请看文档)
  147. Text {
  148.     text: "text"
  149. }
  150. 8. TextInput 下面是设置一个输入文本框,框中的字符串是Text, 并设置鼠标可以选择文本
  151. TextInput{
  152.     text: "Text"
  153.     selectByMouse: true; // 鼠标可以选择
  154. }
  155. 9.IntValidator int 型验证器,和输入框结合后就是只能输入整型数据
  156. TextInput{
  157.     // 最高可以输入100, 最低输入10
  158.     IntValidator{id: intval; bottom: 10; top: 100;}
  159.     width: 100; height: 20;
  160.     text: "";
  161.     // 使用校验器
  162.     validator: intval;
  163. }
  164. 10. DoubleValidator 只能输入浮点数
  165. TextInput{
  166.     // 最高可以输入100, 最低输入10 decimals最多有多少位小数
  167.     // notation 表示是使用科学计数法还是(默认),还是直接的小数 当获取里面的数据
  168.     DoubleValidator{id: intval; decimals: 4; bottom: 10; top: 100; notation:DoubleValidator.StandardNotation}
  169.     width: 100; height: 20;
  170.     text: "";
  171.     // 使用校验器
  172.     validator: intval;
  173. }
  174. 11. RegExpValidator 使用正则表达式
  175. TextInput{
  176.     // 使用一个正则表达式来控制输入的字符串
  177.     // /^[a-zA-Z]{1}[0-1]{0,2}[a-z]{1,3}$/ 表示 开始位置必须是一个大写或小写字母
  178.     // 接下来是0~2个的数字而且是0或1,在接下来是1~3个的小写字母
  179.     RegExpValidator{id: intval; regExp:/^[a-zA-Z]{1}[0-1]{0,2}[a-z]{1,3}$/;}
  180.     width: 100; height: 20;
  181.     text: "";
  182.     // 使用校验器
  183.     validator: intval;
  184. }
  185. 12. TextEdit
  186. 显示一段hello world的html文本和TextInput相同

  187. TextEdit {
  188.     width: 240
  189.     text: "Hello World!"
  190.     font.family: "Helvetica"
  191.     font.pointSize: 20
  192.     color: "blue"
  193.     focus: true
  194. }

  195. 13. MouseArea

  196. 主要是用来判断鼠标事件的区域
  197. Rectangle{
  198.     x: 0; y: 0;
  199.     width: 100; height:100;
  200.     Rectangle{
  201.         id: mousrect
  202.         x: 20; y: 20;
  203.         width: 20; height: 20;
  204.         color: "blue"
  205.         MouseArea{
  206.             // 使用父的区域作为鼠标判断的区域及 x: 20; y: 20; width: 20; height: 20;
  207.             anchors.fill: parent;
  208.             // 但鼠标按下后 mousrect变成红色,当鼠标松开后变成蓝色
  209.             onPressed: { mousrect.color = "red";}
  210.             onReleased: { mousrect.color = "blue";}
  211.         }
  212.     }
  213. }


  214. 14. FocusScope
  215. 不是很清楚说的什么,好像是说同一个时刻只有一个item有焦点
  216. 15. Flickable
  217. 显示一个200x200的框,框中显示图片上200x200的部分
  218. Flickable {
  219.     width: 200; height: 200
  220.     // 设置使用图片的宽 高,而现实的是 200x200的现实框
  221.     contentWidth: image.width; contentHeight: image.height
  222.     Image { id: image; source: "../Images/need.png" }
  223. }

  224. 16. Flipable
  225. 包含两个面,一个前面,一个后面,实现一个控件前后的翻转效果,并且在后面可以添加一些控制

  226. Flipable {
  227.     id: flipable
  228.     width: 240
  229.     height: 240
  230.     property int angle: 0
  231.     property bool flipped: false
  232.     front: Image { source: "front.png" } // 前面
  233.     back: Image { source: "back.png" } // 后面
  234.     // 旋转动画 前后面交换
  235.     transform: Rotation {
  236.         origin.x: flipable.width/2; origin.y: flipable.height/2
  237.         axis.x: 0; axis.y: 1; axis.z: 0 // rotate around y-axis
  238.         angle: flipable.angle
  239.     }
  240.     states: State {
  241.         name: "back"
  242.         PropertyChanges { target: flipable; angle: 180 }
  243.         when: flipable.flipped
  244.     }
  245.     transitions: Transition {
  246.         NumberAnimation { properties: "angle"; duration: 1000 }
  247.     }
  248.     MouseArea {
  249.         anchors.fill: parent
  250.         onClicked: flipable.flipped = !flipable.flipped
  251.     }
  252. }


  253. 17. State


  254. // 当鼠标按下后改变 myRect 的颜色
  255. Rectangle {
  256.     id: myRect
  257.     width: 100; height: 100
  258.     color: "black"
  259.     MouseArea {
  260.         id: mouseArea
  261.         anchors.fill: parent
  262.         onClicked: myRect.state == 'clicked' ? myRect.state = "" : myRect.state = 'clicked';
  263.     }
  264.     // 设置状态
  265.     states: [
  266.         State {
  267.             name: "clicked"
  268.             PropertyChanges { target: myRect; color: "red" }
  269.         }
  270.     ]
  271. }


  272. 18. PropertyChanges

  273. // 当鼠标按下后改变状态
  274. // 状态里面的属性改变包含了文本和颜色的改变
  275. Text {
  276.     id: myText
  277.     width: 100; height: 100
  278.     text: "Hello"
  279.     color: "blue"
  280.     states: State {
  281.         name: "myState"
  282.         // 当这个状态被设置的时候,将改变myText的文本和颜色
  283.         PropertyChanges {
  284.             target: myText
  285.             text: "Goodbye"
  286.             color: "red"
  287.         }
  288.     }
  289.     MouseArea { anchors.fill: parent; onClicked: myText.state = 'myState' }
  290. }


  291. 19. StateGroup

  292. 一个状态组中可以包含很多的状态和变化,而状态也可以和变换绑定.



  293. 20. StateChangeScript

  294. 在状态中可以对脚本中的函数进行调用
  295. // Sc.js
  296. function changeColor() // 返回蓝色
  297. {
  298.     return "blue";
  299. }

  300. // test.qml
  301. import "Sc.js" as Code
  302. Rectangle{
  303.     id: rect
  304.     width: 50; height: 50
  305.     color: "red"
  306.     MouseArea {
  307.         anchors.fill: parent
  308.         onClicked: rect.state = "first" // 鼠标按下改变状态
  309.     }
  310.     states: State {name: "first";
  311.         StateChangeScript{
  312.             name: "myScript";
  313.             script: rect.color = Code.changeColor();
  314.         }
  315.     }
  316. }


  317. 21. ParentChang
  318. 把指定的item换一个item父节点

  319. Item {
  320.     width: 200; height: 100
  321.     Rectangle {
  322.         id: redRect
  323.         width: 100; height: 100
  324.         color: "red"
  325.     }
  326.     // 本来blueRect的父节点是Item 当鼠标按下后他被设置到 redRect上
  327.     Rectangle {
  328.         id: blueRect
  329.         x: redRect.width
  330.         width: 50; height: 50
  331.         color: "blue"
  332.         states: State {
  333.             name: "reparented"
  334.             // 改变父节点
  335.             ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
  336.         }
  337.         MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
  338.     }
  339. }


  340. 22. AnchorChanges

  341. Rectangle {
  342.     id: window
  343.     width: 120; height: 120
  344.     color: "black"
  345.     Rectangle { id: myRect; width: 50; height: 50; color: "red" }
  346.     states: State {
  347.         name: "reanchored"
  348.         AnchorChanges { // 改变 myRect 的anchors属性
  349.             target: myRect
  350.             anchors.top: window.top
  351.             anchors.bottom: window.bottom
  352.         }
  353.         PropertyChanges {
  354.             target: myRect
  355.             anchors.topMargin: 10
  356.             anchors.bottomMargin: 10
  357.         }
  358.     }
  359.     // 鼠标事件
  360.     MouseArea { anchors.fill: parent; onClicked: window.state = "reanchored" }
  361. }


  362. 23. Behavior

  363. Rectangle {
  364.     id: rect
  365.     width: 100; height: 100
  366.     color: "red"
  367.     // 针对宽度的动画
  368.     Behavior on width {
  369.         NumberAnimation { duration: 1000 }
  370.     }
  371.     MouseArea {
  372.         anchors.fill: parent
  373.         onClicked: rect.width = 50
  374.     }
  375. }

  376. 24. SequentialAnimation
  377. 串行播放多个动画
  378. Rectangle {
  379.     id: rect1
  380.     width: 500; height: 500
  381.     Rectangle{
  382.         id: rect;
  383.         color: "red"
  384.         width: 100; height: 100
  385.         // 串行播放多个动画,先横向移动,在纵向移动
  386.         SequentialAnimation{
  387.             running: true;
  388.             NumberAnimation {target:rect; properties:"x"; to: 50; duration: 1000 }
  389.             NumberAnimation {target:rect; properties:"y"; to: 50; duration: 1000 }
  390.         }
  391.     }
  392. }


  393. 25. ParallelAnimation

  394. Rectangle {
  395.     id: rect1
  396.     width: 500; height: 500
  397.     Rectangle{
  398.         id: rect;
  399.         color: "red"
  400.         width: 100; height: 100
  401.         // 并行播放动画,同时横向和纵向移动
  402.         ParallelAnimation{
  403.             running: true;
  404.             NumberAnimation {target:rect; properties:"x"; to: 50; duration: 1000 }
  405.             NumberAnimation {target:rect; properties:"y"; to: 50; duration: 1000 }
  406.         }
  407.     }
  408. }


  409. 26. PropertyAnimation

  410. Rectangle {
  411.     id: rect
  412.     width: 100; height: 100
  413.     color: "red"
  414.     states: State {
  415.         name: "moved"
  416.         PropertyChanges { target: rect; x: 50 }
  417.     }
  418.     transitions: Transition {
  419.         // 属性动画 这里是当属性 x或y发生变化的时候,就播放这样一个动画
  420.         PropertyAnimation { properties: "x,y"; easing.type: Easing.InOutQuad }
  421.     }
  422.     MouseArea{
  423.         anchors.fill: parent;
  424.         onClicked: rect.state = "moved";
  425.     }
  426. }


  427. 27. NumberAnimation

  428. Rectangle {
  429.     width: 100; height: 100
  430.     color: "red"
  431.     // 对当前item的x进行移动,目标移动到x = 50
  432.     NumberAnimation on x { to: 50; duration: 1000 }
  433. }


  434. 28. Vector3dAnimation



  435. 29. ColorAnimation

  436. 颜色的过度
  437. Rectangle {
  438.     width: 100; height: 100
  439.     color: "red"
  440.     ColorAnimation on color { to: "yellow"; duration: 1000 }
  441. }


  442. 30. RotationAnimation

  443. 默认是绕z轴进行的旋转
  444. Item {
  445.     width: 300; height: 300
  446.     Rectangle {
  447.         id: rect
  448.         width: 150; height: 100; anchors.centerIn: parent
  449.         color: "red"
  450.         smooth: true
  451.         states: State {
  452.             name: "rotated"; PropertyChanges { target: rect; rotation: 180 }
  453.         }
  454.         transitions: Transition {
  455.             RotationAnimation { duration: 1000; direction: RotationAnimation.Counterclockwise }
  456.         }
  457.     }
  458.     MouseArea { anchors.fill: parent; onClicked: rect.state = "rotated" }
  459. }


  460. 31. ParentAnimation

  461. 一个切换父节点的动画,平滑的过度
  462. Item {
  463.     width: 200; height: 100
  464.     Rectangle {
  465.         id: redRect
  466.         width: 100; height: 100
  467.         color: "red"
  468.     }
  469.     Rectangle {
  470.         id: blueRect
  471.         x: redRect.width
  472.         width: 50; height: 50
  473.         color: "blue"
  474.         states: State {
  475.             name: "reparented"
  476.             ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
  477.         }
  478.         transitions: Transition {
  479.             ParentAnimation {
  480.                 NumberAnimation { properties: "x,y"; duration: 1000 }
  481.             }
  482.         }
  483.         MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
  484.     }
  485. }

  486. 32. AnchorAnimation

  487. Item {
  488.     id: container
  489.     width: 200; height: 200
  490.     Rectangle {
  491.         id: myRect
  492.         width: 100; height: 100
  493.         color: "red"
  494.     }
  495.     states: State {
  496.         name: "reanchored"
  497.         AnchorChanges { target: myRect; anchors.right: container.right }
  498.     }
  499.     transitions: Transition {
  500.         // smoothly reanchor myRect and move into new position
  501.         AnchorAnimation { duration: 1000 }
  502.     }
  503.     // 当控件加载完成后
  504.     Component.onCompleted: container.state = "reanchored"
  505. }

  506. 33. PauseAnimation

  507. 延迟效果
  508. Item {
  509.     id: container
  510.     width: 200; height: 200
  511.     Rectangle {
  512.         id: myRect
  513.         width: 100; height: 100
  514.         color: "red"
  515.         SequentialAnimation {
  516.             running: true;
  517.             NumberAnimation {target: myRect;to: 50; duration: 1000; properties: "x"; }
  518.             PauseAnimation { duration: 5000 } // 延迟100毫秒
  519.             NumberAnimation {target: myRect; to: 50; duration: 1000; properties: "y"; }
  520.         }
  521.     }
  522. }


  523. 34. SmoothedAnimation

  524. 平滑过度
  525. Rectangle {
  526.     width: 800; height: 600
  527.     color: "blue"
  528.     Rectangle {
  529.         width: 60; height: 60
  530.         x: rect1.x - 5; y: rect1.y - 5
  531.         color: "green"
  532.         Behavior on x { SmoothedAnimation { velocity: 200 } }
  533.         Behavior on y { SmoothedAnimation { velocity: 200 } }
  534.     }
  535.     Rectangle {
  536.         id: rect1
  537.         width: 50; height: 50
  538.         color: "red"
  539.     }
  540.     focus: true
  541.     Keys.onRightPressed: rect1.x = rect1.x + 100
  542.     Keys.onLeftPressed: rect1.x = rect1.x - 100
  543.     Keys.onUpPressed: rect1.y = rect1.y - 100
  544.     Keys.onDownPressed: rect1.y = rect1.y + 100
  545. }


  546. 35. SpringAnimation

  547. 平滑的过度过程,在动画结束的时候有种弹性的效果

  548. Item {
  549.     width: 300; height: 300
  550.     Rectangle {
  551.         id: rect
  552.         width: 50; height: 50
  553.         color: "red"
  554.         Behavior on x { SpringAnimation { spring: 2; damping: 0.} }
  555.         Behavior on y { SpringAnimation { spring: 2; damping: 0.} }
  556.     }
  557.     MouseArea {
  558.         anchors.fill: parent
  559.         onClicked: {
  560.             rect.x = mouse.x - rect.width/2
  561.             rect.y = mouse.y - rect.height/2
  562.         }
  563.     }
  564. }


  565. 36. PropertyAction

  566. 主要是在动画过程中直接的改变一个属性
  567. transitions: Transition {
  568.     ...
  569.     PropertyAction { target: theImage; property: "smooth"; value: true }
  570.     ...
  571. }

  572. 38. ScriptAction

  573. 在动画过程中嵌入脚本的调用
  574. SequentialAnimation {
  575.     NumberAnimation { ... }
  576.     ScriptAction { script: doSomething(); }
  577.     NumberAnimation { ... }
  578. }


  579. 39. Transition

  580. Rectangle {
  581.     id: rect
  582.     width: 100; height: 100
  583.     color: "red"
  584.     MouseArea {
  585.         id: mouseArea
  586.         anchors.fill: parent
  587.     }
  588.     states: State {
  589.         name: "moved"; when: mouseArea.pressed
  590.         PropertyChanges { target: rect; x: 50; y: 50 }
  591.     }
  592.     transitions: Transition {
  593.         NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad }
  594.     }
  595. }


  596. 40. Binding

  597. Item {
  598.     width: 300; height: 300
  599.     Text {id: app; text: "xxxfa"}
  600.     TextEdit { id: myTextField; text: "Please type here..." }
  601.     // 把myTextField和app的enteredText属性进行绑定
  602.     Binding { target: app; property: "enteredText"; value: myTextField.text }
  603. }


  604. 41. ListModel

  605. 直接看效果
  606. Rectangle {
  607.     width: 200; height: 200
  608.     ListModel {
  609.         id: fruitModel
  610.         ListElement {
  611.             name: "Apple"
  612.             cost: 2.45
  613.         }
  614.         ListElement {
  615.             name: "Orange"
  616.             cost: 3.25
  617.         }
  618.         ListElement {
  619.             name: "Banana"
  620.             cost: 1.95
  621.         }
  622.     }
  623.     Component {
  624.         id: fruitDelegate
  625.         Row {
  626.             spacing: 10
  627.             Text { text: name }
  628.             Text { text: '$' + cost }
  629.         }
  630.     }
  631.     ListView {
  632.         anchors.fill: parent
  633.         model: fruitModel
  634.         delegate: fruitDelegate
  635.     }
  636. }

  637. 42. ListElement

  638. 请参照ListModel

  639. 43. VisualItemModel

  640. 把可视化图元添加到链表试图
  641. Rectangle {
  642.     width: 100; height: 100;
  643.     VisualItemModel {
  644.         id: itemModel
  645.         Rectangle { height: 30; width: 80; color: "red" }
  646.         Rectangle { height: 30; width: 80; color: "green" }
  647.         Rectangle { height: 30; width: 80; color: "blue" }
  648.     }
  649.     ListView {
  650.         anchors.fill: parent
  651.         model: itemModel
  652.     }
  653. }


  654. 44. VisualDataModel

  655. 看下面效果
  656. Rectangle {
  657.     width: 200; height: 100
  658.     VisualDataModel {
  659.         id: visualModel
  660.         model: ListModel {
  661.             ListElement { name: "Apple" }
  662.             ListElement { name: "Orange" }
  663.         }
  664.         delegate: Rectangle {
  665.             height: 25
  666.             width: 100
  667.             Text { text: "Name: " + name}
  668.         }
  669.     }
  670.     ListView {
  671.         anchors.fill: parent
  672.         model: visualModel
  673.     }
  674. }


  675. 45. Package

  676. 具体请参考
  677. declarative/modelviews/package


  678. 46. XmlListModel XmlRole


  679. 从网络获取xml,暂时没有测试成功

  680. 47. ListView

  681. 参考 ListModel VisualDataModel


  682. 48. GridView

  683. 看效果
  684. Rectangle {
  685.     width: 200; height: 400;
  686.     ListModel {
  687.         id: fruitModel
  688.         ListElement {
  689.             name: "Apple"
  690.             cost: 2.45
  691.         }
  692.         ListElement {
  693.             name: "Orange"
  694.             cost: 3.25
  695.         }
  696.         ListElement {
  697.             name: "Banana"
  698.             cost: 1.95
  699.         }
  700.     }
  701.     GridView {
  702.         anchors.fill: parent
  703.         model: fruitModel
  704.         delegate: Column{
  705.             Text {text:"name" + name}
  706.             Text {text:"cost"+ cost}
  707.         }
  708.     }
  709. }



  710. 49.PathView Path

  711. 看例子

  712. Rectangle {
  713.     width: 200; height: 400;
  714.     ListModel {
  715.         id: fruitModel
  716.         ListElement {
  717.             name: "Apple"
  718.             cost: 2.45
  719.         }
  720.         ListElement {
  721.             name: "Orange"
  722.             cost: 3.25
  723.         }
  724.         ListElement {
  725.             name: "Banana"
  726.             cost: 1.95
  727.         }
  728.     }
  729.     PathView {
  730.         anchors.fill: parent
  731.         model: fruitModel
  732.         delegate: Column{
  733.             Text {text:"name" + name}
  734.             Text {text:"cost"+ cost}
  735.         }
  736.         path:Path{
  737.             startX: 120; startY: 100
  738.             PathQuad { x: 120; y: 25; controlX: 260; controlY: 75 }
  739.             PathQuad { x: 120; y: 100; controlX: -20; controlY: 75 }
  740.         }
  741.     }
  742. }



  743. 50.PathLine

  744. 具体的看运行的例子

  745. Rectangle {
  746.     width: 200; height: 400;
  747.     ListModel {
  748.         id: fruitModel
  749.         ListElement {
  750.             name: "Apple"
  751.             cost: 2.45
  752.         }
  753.         ListElement {
  754.             name: "Orange"
  755.             cost: 3.25
  756.         }
  757.         ListElement {
  758.             name: "Banana"
  759.             cost: 1.95
  760.         }
  761.     }
  762.     PathView {
  763.         anchors.fill: parent
  764.         model: fruitModel
  765.         delegate: Column{
  766.             Text {text:"name" + name}
  767.             Text {text:"cost"+ cost}
  768.         }
  769.         path:Path{
  770.             startX: 150; startY: 120
  771.             PathLine { x: 200; y: 80; }
  772.             PathLine { x: 100; y: 80; }
  773.             PathLine { x: 150; y: 120; }
  774.         }
  775.     }
  776. }

  777. 51.PathQuad

  778. 参考 PathView Path

  779. 52.PathCubic


  780. 还要看

  781. 53.PathAttribute

  782. 可以直接针对一些属性进行改变
  783. Rectangle {
  784.     width: 200; height: 400;
  785.     ListModel {
  786.         id: fruitModel
  787.         ListElement {
  788.             name: "Apple"
  789.             cost: 2.45
  790.         }
  791.         ListElement {
  792.             name: "Orange"
  793.             cost: 3.25
  794.         }
  795.         ListElement {
  796.             name: "Banana"
  797.             cost: 1.95
  798.         }
  799.     }
  800.     PathView {
  801.         anchors.fill: parent
  802.         model: fruitModel
  803.         delegate:
  804.         Item {
  805.             id: delitem;
  806.             width: 80; height: 80;
  807.             Column{
  808.                 // 这里使用图片试试
  809.                 Rectangle {
  810.                     width: 40; height: 40;
  811.                     scale: delitem.scale;
  812.                     color: "red"
  813.                 }
  814.                 Text {text:"name" + name}
  815.                 Text {text:"cost"+ cost}
  816.             }
  817.         }
  818.         //
  819.         path: Path {
  820.             startX: 120; startY: 100
  821.             PathAttribute { name: "Scale"; value: 1.}
  822.             PathQuad { x: 120; y: 25; controlX: 260; controlY: 75 }
  823.             PathAttribute { name: "Scale"; value: 0.}
  824.             PathQuad { x: 120; y: 100; controlX: -20; controlY: 75 }
  825.         }
  826.     }
  827. }

  828. 54.PathPercent

  829. 具体请看QML文档


  830. 55.WebView

  831. import QtWebKit 1.0
  832. WebView {
  833.     url: "http://www.nokia.com"
  834.     preferredWidth: 490
  835.     preferredHeight: 400
  836.     scale: 0.5
  837.     smooth: false
  838. }


  839. 56 Column
  840. 横向排列
  841. Rectangle{
  842.     width: 100; height: 100;
  843.     // 纵向排列
  844.     Column {
  845.         spacing: 2
  846.         Rectangle { color: "red"; width: 50; height: 50 }
  847.         Rectangle { color: "green"; width: 20; height: 50 }
  848.         Rectangle { color: "blue"; width: 50; height: 20 }
  849.     }
  850. }


  851. 57 Row

  852. Rectangle{
  853.     width: 100; height: 100;
  854.     // 横向排列
  855.     Row {
  856.         spacing: 2
  857.         Rectangle { color: "red"; width: 50; height: 50 }
  858.         Rectangle { color: "green"; width: 20; height: 50 }
  859.         Rectangle { color: "blue"; width: 50; height: 20 }
  860.     }
  861. }


  862. 58 Grid

  863. Rectangle{
  864.     width: 100; height: 100;
  865.     // 网格排列
  866.     Grid {
  867.         columns: 3
  868.         spacing: 2
  869.         Rectangle { color: "red"; width: 50; height: 50 }
  870.         Rectangle { color: "green"; width: 20; height: 50 }
  871.         Rectangle { color: "blue"; width: 50; height: 20 }
  872.         Rectangle { color: "cyan"; width: 50; height: 50 }
  873.         Rectangle { color: "magenta"; width: 10; height: 10 }
  874.     }
  875. }

  876. 59 Flow

  877. Rectangle{
  878.     width: 100; height: 100;
  879.     // 网格排列
  880.     Flow {
  881.         spacing: 2
  882.         width: 100; height: 100;
  883.         Rectangle { color: "red"; width: 50; height: 50 }
  884.         Rectangle { color: "green"; width: 20; height: 50 }
  885.         Rectangle { color: "blue"; width: 50; height: 20 }
  886.         Rectangle { color: "cyan"; width: 50; height: 50 }
  887.         Rectangle { color: "magenta"; width: 10; height: 10 }
  888.     }
  889. }

  890. 60 Connections

  891. 下面是3中情况下会使用的,具体的不好翻译
  892. Multiple connections to the same signal are required
  893. 有多个连接要连接到相同的信号时
  894. Creating connections outside the scope of the signal sender
  895. 创建的连接在范围之外
  896. Connecting to targets not defined in QML
  897. 创建的连接没有在QML中定义的

  898. Rectangle{
  899.     width: 100; height: 100;
  900.     MouseArea {
  901.         id: area
  902.         anchors.fill: parent;
  903.     }
  904.     Connections {
  905.         target: area
  906.         onClicked: { console.log(" ok");}
  907.     }
  908. }


  909. 61 Component

  910. 组件是可以重用的QML元素,具体还是看QML的文档翻译不是很好

  911. Item {
  912.     width: 100; height: 100
  913.     // 定义一个组件他包含一个10x10的红色矩形
  914.     Component {
  915.         id: redSquare
  916.         Rectangle {
  917.             color: "red"
  918.             width: 10
  919.             height: 10
  920.         }
  921.     }
  922.     // 动态的载入一个组件
  923.     Loader { sourceComponent: redSquare }
  924.     Loader { sourceComponent: redSquare; x: 20 }
  925. }


  926. 62 Timer

  927. Item {
  928.     width: 200; height: 40;
  929.     // 和QTimer 差不多
  930.     Timer {
  931.         interval: 500; running: true; repeat: true
  932.         onTriggered: time.text = Date().toString() // 使用javascript获取系统时间
  933.     }
  934.     Text { id: time }
  935. }


  936. 63 QtObject

  937. 他是不可见的只有objectName一个属性
  938. 通过这个属性我们可以在c++中找到我们想要的对象

  939. // MyRect.qml
  940. import Qt 4.7
  941. Item {
  942.     width: 200; height: 200
  943.     Rectangle {
  944.         anchors.fill: parent
  945.         color: "red"
  946.         objectName: "myRect"
  947.     }
  948. }

  949. // main.cpp
  950. QDeclarativeView view;
  951. view.setSource(QUrl::fromLocalFile("MyRect.qml"));
  952. view.show();
  953. QDeclarativeItem *item = view.rootObject()->findChild<QDeclarativeItem*>("myRect");
  954. if (item)
  955.     item->setProperty("color", QColor(Qt::yellow));



  956. 64 Qt

  957. 提供全局有用的函数和枚举,具体的看QML文档


  958. 65. WorkerScript

  959. 使用它可以把操作放到一个新的线程中,使得它在后台运行而不影响主GUI
  960. 具体可以看QML中它的文档

  961. 66. Loader

  962. 可以参考Component
  963. 还有QML中的文档


  964. 67 Repeater

  965. 他可以创建很多相似的组件,QML中还有几个例子

  966. Row {
  967.     Repeater {
  968.         model: 3
  969.         Rectangle {
  970.             width: 100; height: 40
  971.             border.width: 1
  972.             color: "yellow"
  973.         }
  974.     }
  975. }

  976. 68 SystemPalette

  977. 具体看效果和文档
  978. Rectangle {
  979.     SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
  980.     width: 640; height: 480
  981.     color: myPalette.window
  982.     Text {
  983.         anchors.fill: parent
  984.         text: "Hello!"; color: myPalette.windowText
  985.     }
  986. }


  987. 69. FontLoader


  988. 载入一种字体,可以是网络上的,也可以是本地的
  989. Column {
  990.     FontLoader { id: fixedFont; name: "Courier" }
  991.     FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" }
  992.     Text { text: "Fixed-size font"; font.family: fixedFont.name }
  993.     Text { text: "Fancy font"; font.family: webFont.name }
  994. }

  995. 70 LayoutItem

  996. 不清楚

  997. 71 Scale

  998. 对缩放的控制
  999. Rectangle {
  1000.     width: 100; height: 100
  1001.     color: "blue"
  1002.     Rectangle{
  1003.         x: 50; y: 50;
  1004.         width: 20; height: 20;
  1005.         color: "red"
  1006.         // 这里是在当前矩形的中间位置沿x轴进行3倍缩放
  1007.         transform: Scale { origin.x: 10; origin.y: 10; xScale: 3}
  1008.     }
  1009. }


  1010. 72 Rotation
  1011. Rectangle {
  1012.     width: 100; height: 100
  1013.     color: "blue"
  1014.     // 绕位置25,25 旋转45度
  1015.     transform: Rotation { origin.x: 25; origin.y: 25; angle: 45}
  1016. }


  1017. 73 Translate

  1018. Row {
  1019.     Rectangle {
  1020.         width: 100; height: 100
  1021.         color: "blue"
  1022.         // 沿y轴正方向移动20个像素
  1023.         transform: Translate { y: 20 }
  1024.     }
  1025.     Rectangle {
  1026.         width: 100; height: 100
  1027.         color: "red"
  1028.         // 沿y轴负方向移动20个像素
  1029.         transform: Translate { y: -20 }
  1030.     }
  1031. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值