当前位置:   article > 正文

14、ListView案例_listview 放大

listview 放大

一、介绍

通过ListView实现图片点击放大并且添加说明的功能

 

二、实现

  1. import QtQuick 2.5
  2. import QtQuick.Window 2.3
  3. Window {
  4. visible: true
  5. id:root
  6. width: 300
  7. height: 500
  8. ListView {
  9. id:lv
  10. anchors.fill: parent
  11. model:lmod//数据模型
  12. delegate: showdele//显示样式
  13. }
  14. ListModel {
  15. id:lmod
  16. ListElement {
  17. name: "Mercury"//这三个属性命名随意
  18. imagesource: "images/mercury.jpeg"
  19. facts: "Mercury is the smallest planet in the Solar System.
  20. It is the closest planet to the sun.
  21. It makes one trip around the Sun once every 87.969 days."
  22. }
  23. ListElement {
  24. name: "Venus"
  25. imagesource: "images/venus.jpeg"
  26. facts: "Venus is the second planet from the Sun.
  27. It is a terrestrial planet because it has a solid, rocky surface.
  28. The other terrestrial planets are Mercury, Earth and Mars. Astronomers have known Venus for thousands of years."
  29. }
  30. ListElement {
  31. name: "Earth"
  32. imagesource: "images/earth.jpeg"
  33. facts: "The Earth is the third planet from the Sun.
  34. It is one of the four terrestrial planets in our Solar System.
  35. This means most of its mass is solid. The other three are Mercury, Venus and Mars.
  36. The Earth is also called the Blue Planet, 'Planet Earth', and 'Terra'."
  37. }
  38. ListElement {
  39. name: "Mars"
  40. imagesource: "images/mars.jpeg"
  41. facts: "Mars is the fourth planet from the Sun in the Solar System.
  42. Mars is dry, rocky and cold. It is home to the largest volcano in the Solar System.
  43. Mars is named after the mythological Roman god of war because it is a red planet, which signifies the colour of blood."
  44. }
  45. }
  46. Component {
  47. id:showdele
  48. Item {
  49. id: wrapper
  50. width: lv.width
  51. height: 30
  52. Rectangle {//负责显示name
  53. id:charactor
  54. anchors.left: parent.left
  55. anchors.right: parent.right
  56. color: "black"
  57. height: 30
  58. Text {
  59. anchors.left: parent.left
  60. anchors.leftMargin: 5
  61. anchors.topMargin: 5
  62. anchors.verticalCenter: parent.verticalCenter
  63. color: "white"
  64. text: name
  65. }
  66. }
  67. Rectangle {//负责显示图片
  68. id:img
  69. width: 26
  70. height: 26
  71. anchors.right: parent.right
  72. anchors.verticalCenter: parent.verticalCenter
  73. anchors.rightMargin: 2
  74. Image {
  75. anchors.fill: parent
  76. source: imagesource
  77. }
  78. }
  79. MouseArea {//当点击item区域时,将item的状态置位expand
  80. anchors.fill: parent
  81. onClicked: {
  82. parent.state="expand"
  83. }
  84. }
  85. Item {//负责显示文字说明
  86. id: factview
  87. anchors.top: img.bottom
  88. anchors.left: parent.left
  89. anchors.right: parent.right
  90. anchors.bottom: parent.bottom
  91. opacity: 0
  92. Rectangle {
  93. anchors.fill: parent
  94. Text {
  95. anchors.fill: parent
  96. anchors.margins: 4
  97. clip: true
  98. text:facts
  99. }
  100. }
  101. }
  102. Rectangle {
  103. id:closebtn
  104. color: "blue"
  105. anchors.right: parent.right
  106. anchors.top: parent.top
  107. anchors.topMargin: 4
  108. anchors.rightMargin: 4
  109. height: 26
  110. width: 26
  111. opacity: 0
  112. }
  113. MouseArea {//点击closebtn后,状态清除
  114. anchors.fill: closebtn
  115. onClicked: {
  116. parent.state=""
  117. }
  118. }
  119. states: [
  120. State {
  121. name: "expand"//当状态为expand时,各个属性应该如何变化
  122. PropertyChanges { target: wrapper; height: lv.height }//wrapper的height变成ListView的height,lv.height为500,这样就会使单个条目充满整个界面
  123. PropertyChanges { target: img; width: lv.width; height: lv.width; anchors.rightMargin: 0; anchors.topMargin: 30 }//图片放大到ListView的宽度和高度
  124. PropertyChanges { target: factview; opacity: 1 }//文字和按钮变成不透明显示出来
  125. PropertyChanges { target: closebtn; opacity: 1 }
  126. PropertyChanges { target: wrapper.ListView.view; contentY: wrapper.y; interactive: false }//wrapper.y的值为0,这样,点击后,所选条目就能位于最上方,且高度是500
  127. //interactive表示用户无法上拉下拉,无法和界面交互
  128. }
  129. ]
  130. transitions: [//定义过渡动画
  131. Transition {
  132. NumberAnimation {
  133. duration: 500;//定义属性变化时的动画时间
  134. properties: "height,width,anchors.rightMargin,anchors.topMargin,opacity,contentY"//添加动画的属性
  135. }
  136. }
  137. ]
  138. }
  139. }
  140. }

 

参考

《qml book》

 

作者水平有限,如有错误,欢迎指出

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/764756
推荐阅读
相关标签
  

闽ICP备14008679号