当前位置:   article > 正文

qml实现点击按钮往Columnt添加一个组件对象

qml实现点击按钮往Columnt添加一个组件对象
  1. import QtQuick 2.0
  2. import QtQuick.Window 2.0
  3. import QtQuick.Controls 1.4
  4. Window {
  5. width: 800
  6. height: 600
  7. visible: true
  8. Rectangle {
  9. id: mainRect
  10. anchors.fill: parent
  11. property var mainRectComponent: null
  12. Column {
  13. id: mainColumn
  14. spacing: 5
  15. width: parent.width
  16. property var count: 0
  17. function deleteItems(object) {
  18. object.destroy()
  19. }
  20. function createItem() {
  21. var color = 'red'
  22. if (mainColumn.count % 3 === 1)
  23. color = 'yellow'
  24. else if (mainColumn.count % 3 === 2)
  25. color = 'blue'
  26. mainColumn.count++
  27. // 创建一个组件
  28. var obj = itemCompont.createObject(mainColumn, {color: color, width: mainRect.width,height:100})
  29. //obj.setCurentObject(obj)
  30. obj.val++
  31. obj.setCurrentText('Component' + mainColumn.count.toString()+ " : "+obj.val)
  32. obj.deleteThis.connect(mainColumn.deleteItems)
  33. }
  34. }
  35. Button {
  36. anchors.top: mainColumn.bottom
  37. anchors.topMargin: 10
  38. anchors.right: mainRect.right
  39. anchors.rightMargin: 10
  40. text: '添加'
  41. onClicked: {
  42. mainColumn.createItem()
  43. }
  44. }
  45. Component {
  46. id: itemCompont
  47. Rectangle {
  48. id: compontRect
  49. color: 'blue'
  50. implicitWidth: 20
  51. implicitHeight: 50
  52. property var currentObject: ''
  53. property int val: 5
  54. signal deleteThis(var obj)
  55. // 设置文字的内容
  56. function setCurrentText(textName) {
  57. interText.text = textName
  58. }
  59. Text {
  60. id: interText
  61. anchors.left: parent.left
  62. anchors.leftMargin: 10
  63. anchors.verticalCenter: parent.verticalCenter
  64. text: qsTr("text")
  65. }
  66. Button {
  67. anchors.margins: 5
  68. anchors.top: parent.top
  69. anchors.bottom: parent.bottom
  70. anchors.right: parent.right
  71. text: '删除'
  72. onClicked: {
  73. compontRect.deleteThis(compontRect)
  74. }
  75. }
  76. Component.onCompleted: {
  77. compontRect.currentObject = parent
  78. }
  79. }
  80. }
  81. }
  82. }

运行效果:


FR:徐海涛(hunkxu)

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号