基本元素(Basic Elements)

元素可以被分为可视化元素与非可视化元素。一个可视化元素(例如矩形框Rectangle)有着几何形状并且可以在屏幕上显示。一个非可视化元素(例如计时器Timer)提供了常用的功能,通常用于操作可视化元素。
现在我们将专注于几个基础的可视化元素,例如Item(基础元素对象),Rectangle(矩形框),Text(文本),Image(图像)和MouseArea(鼠标区域)。

基础元素对象(Item Element)

Item(基础元素对象)是所有可视化元素的基础对象,所有其它的可视化元素都继承自Item。它自身不会有任何绘制操作,但是定义了所有可视化元素共有的属性:

Group(分组) Properties(属性)
Geometry(几何属性) x,y(坐标)定义了元素左上角的位置,width,height(长和宽)定义元素的显示范围,z(堆叠次序)定义元素之间的重叠顺序。
Layout handling(布局操作) anchors(锚定),包括左(left),右(right),上(top),下(bottom),水平与垂直居中(vertical center,horizontal center),与margins(间距)一起定义了元素与其它元素之间的位置关系。
Key handlikng(按键操作) 附加属性key(按键)和keyNavigation(按键定位)属性来控制按键操作,处理输入焦点(focus)可用操作。
Transformation(转换) 缩放(scale)和rotate(旋转)转换,通用的x,y,z属性列表转换(transform),旋转基点设置(transformOrigin)。
Visual(可视化) 不透明度(opacity)控制透明度,visible(是否可见)控制元素是否显示,clip(裁剪)用来限制元素边界的绘制,smooth(平滑)用来提高渲染质量。
State definition(状态定义) states(状态列表属性)提供了元素当前所支持的状态列表,当前属性的改变也可以使用transitions(转变)属性列表来定义状态转变动画。
  1. Properties:
  2. activeFocus : bool
  3. activeFocusOnTab : bool
  4. anchors
  5. anchors.alignWhenCentered : bool
  6. anchors.baseline : AnchorLine
  7. anchors.baselineOffset : real
  8. anchors.bottom : AnchorLine
  9. anchors.bottomMargin : real
  10. anchors.centerIn : Item
  11. anchors.fill : Item
  12. anchors.horizontalCenter : AnchorLine
  13. anchors.horizontalCenterOffset : real
  14. anchors.left : AnchorLine
  15. anchors.leftMargin : real
  16. anchors.margins : real
  17. anchors.right : AnchorLine
  18. anchors.rightMargin : real
  19. anchors.top : AnchorLine
  20. anchors.topMargin : real
  21. anchors.verticalCenter : AnchorLine
  22. anchors.verticalCenterOffset : real
  23. antialiasing : bool
  24. baselineOffset : int
  25. children : list<Item>
  26. childrenRect
  27. childrenRect.height : real
  28. childrenRect.width : real
  29. childrenRect.x : real
  30. childrenRect.y : real
  31. clip : bool
  32. containmentMask : QObject*
  33. data : list<Object>
  34. enabled : bool
  35. focus : bool
  36. height : real
  37. implicitHeight : real
  38. implicitWidth : real
  39. layer.effect : Component
  40. layer.enabled : bool
  41. layer.format : enumeration
  42. layer.mipmap : bool
  43. layer.samplerName : string
  44. layer.samples : enumeration
  45. layer.smooth : bool
  46. layer.sourceRect : rect
  47. layer.textureMirroring : enumeration
  48. layer.textureSize : size
  49. layer.wrapMode : enumeration
  50. opacity : real
  51. parent : Item
  52. resources : list<Object>
  53. rotation : real
  54. scale : real
  55. smooth : bool //平滑过渡,增加抗锯齿
  56. state : string
  57. states : list<State>
  58. transform : list<Transform>
  59. transformOrigin : enumeration
  60. transitions : list<Transition>
  61. visible : bool
  62. visibleChildren : list<Item>
  63. width : real
  64. x : real
  65. y : real
  66. z : real
  67. Methods:
  68. childAt(real x, real y)
  69. bool contains(point point)
  70. forceActiveFocus(Qt::FocusReason reason)
  71. forceActiveFocus()
  72. bool grabToImage(callback, targetSize)
  73. object mapFromGlobal(real x, real y)
  74. object mapFromItem(Item item, rect r)
  75. object mapFromItem(Item item, real x, real y, real width, real height)
  76. object mapFromItem(Item item, real x, real y)
  77. object mapFromItem(Item item, point p)
  78. object mapToGlobal(real x, real y)
  79. object mapToItem(Item item, rect r)
  80. object mapToItem(Item item, real x, real y, real width, real height)
  81. object mapToItem(Item item, real x, real y)
  82. object mapToItem(Item item, point p)
  83. nextItemInFocusChain(bool forward)

注意
Item(基本元素对象)通常被用来作为其它元素的容器使用,类似HTML语言中的div元素(div element)。

矩形框元素(Rectangle Element)

  1. antialiasing : bool 是否启用抗锯齿,当线条显示效果出现锯齿像素时使用
  2. border 矩形边框,边框所占位置是在矩形内部的
  3. border.color : color
  4. border.width : int
  5. color : color 矩形填充颜色
  6. gradient : any 渐变
  7. radius : real 边角弧度
  8. focus:bool 当前元素是否会拥有焦点
  9. rotation : real 旋转
  10. opacity : real 透明度
  11. scale : real 缩放
  12. states : list<State> 状态列表
  13. //设置同级项目的堆叠顺序。默认情况下,堆叠顺序为0。
  14. //堆叠值较高的项目绘制在堆叠顺序较低的同级项目的顶部。具有相同堆叠值的项目按其显示顺序从下至上绘制。堆叠值为负值的项目在其父内容下绘制。
  15. z : real

注意
颜色的命名是来自SVG颜色的名称(查看http://www.w3.org/TR/css3-color/#svg-color可以获取更多的颜色名称)。你也可以使用其它的方法来指定颜色,比如RGB字符串(’#FF4444’),或者一个颜色名字(例如’white’)。
此外,填充的颜色与矩形的边框也支持自定义的渐变色。

  1. Rectangle {
  2. y: 0; width: 80; height: 80
  3. color: "lightsteelblue" //淡钢蓝
  4. border.color: "orange"
  5. border.width: 2
  6. radius: 5
  7. }
  8. Rectangle {
  9. y: 100; width: 80; height: 80
  10. gradient: Gradient {
  11. GradientStop { position: 0.0; color: "lightsteelblue" }
  12. GradientStop { position: 1.0; color: "blue" }
  13. }
  14. opacity:0.5
  15. }
  16. Rectangle {
  17. y: 200; width: 80; height: 80
  18. rotation: 60
  19. gradient: Gradient {
  20. GradientStop { position: 0.0; color: "lightsteelblue" }
  21. GradientStop { position: 1.0; color: "blue" }
  22. }
  23. antialiasing:true
  24. }

image.png一个渐变色是由一系列的梯度值定义的。每一个值定义了一个位置与颜色。位置标记了y轴上的位置(0 = 顶,1 = 底)。GradientStop(倾斜点)的颜色标记了颜色的位置。

  1. Rectangle {
  2. y: 0; width: 80; height: 80
  3. color: "lightsteelblue"
  4. border.color: "orange"
  5. border.width: 2
  6. radius: 5
  7. focus: true
  8. MouseArea{
  9. anchors.fill: parent
  10. onClicked: { //在鼠标区域内进行点击操作即可触发,不需focus设置true
  11. console.log("on clicked")
  12. }
  13. }
  14. Keys.onReturnPressed: { //需focus:true使得矩形获得焦点才可触发
  15. console.log("on Return Pressed")
  16. }
  17. }
  1. Rectangle {
  2. id: root
  3. width: 100; height: 100
  4. state: "normal"
  5. states: [
  6. State {
  7. name: "normal"
  8. PropertyChanges { target: root; color: "green" }
  9. },
  10. State {
  11. name: "red_color"
  12. PropertyChanges { target: root; color: "red" }
  13. },
  14. State {
  15. name: "blue_color"
  16. PropertyChanges { target: root; color: "blue" }
  17. }
  18. ]
  19. MouseArea{
  20. anchors.fill: parent
  21. // onClicked: { //在鼠标区域内进行点击操作即可触发,不需focus设置true
  22. // console.log("on clicked")
  23. // root.state = "red_color"
  24. // }
  25. onPressed: { //在鼠标区域内进行点击操作即可触发,不需focus设置true
  26. console.log("on Pressed")
  27. root.state = "red_color"
  28. }
  29. onReleased: {
  30. console.log("on Released")
  31. root.state = "blue_color"
  32. }
  33. }
  34. }

注意
一个矩形框如果没有width/height(宽度与高度)将不可见。如果你有几个相互关联width/height(宽度与高度)的矩形框,在你组合逻辑中出了错后可能就会发生矩形框不可见,请注意这一点。
注意
这个函数无法创建一个梯形,最好使用一个已有的图像来创建梯形。有一种可能是在旋转梯形时,旋转的矩形几何结构不会发生改变,但是这会导致几何元素相同的可见区域的混淆。从作者的观点来看类似的情况下最好使用设计好的梯形图形来完成绘制。