• Qt基于Qml向左边滑动删除列表项


    从右向左拉,出现删除按钮

    删除记录后

     

    3.主要通过捕获鼠标坐标及属性动画来实现

    鼠标坐标捕获

    1. MouseArea{
    2. property point clickPos: "0,0"
    3. anchors.fill: parent
    4. onPressed: {
    5. clickPos = Qt.point(mouse.x,mouse.y);//取鼠标坐标
    6. }
    7. onReleased: {
    8. var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)//释放鼠标后坐标
    9. if ((delta.x < 0) && (aBtnShow.running === false) && (delBtn.width === 0)){
    10. aBtnShow.start();
    11. }else if (aBtnHide.running === false && (delBtn.width > 0)){
    12. aBtnHide.start();
    13. }
    14. }
    15. }

     属性动画

    1. PropertyAnimation{
    2. id: aBtnShow
    3. target: delBtn
    4. property: "width"
    5. duration: 100
    6. from: 0
    7. to: 60
    8. }
    9. PropertyAnimation{
    10. id: aBtnHide
    11. target: delBtn
    12. property: "width"
    13. duration: 100
    14. from: 60
    15. to: 0
    16. }

    完整QML代码:

    1. import QtQml 2.12
    2. import QtQuick 2.12
    3. import QtQuick.Controls 2.12
    4. ApplicationWindow {
    5. visible: true
    6. width: 800
    7. height: 600
    8. title: qsTr("Qt基于Qml向左边滑动删除列表项")
    9. //列表数据源
    10. ListModel {
    11. id: listModel
    12. ListElement {
    13. text: qsTr("1.向左边滑动出现删除按钮")
    14. }
    15. ListElement {
    16. text: qsTr("2.向左边滑动出现删除按钮")
    17. }
    18. ListElement {
    19. text: qsTr("3.向左边滑动出现删除按钮")
    20. }
    21. ListElement {
    22. text: qsTr("4.向左边滑动出现删除按钮")
    23. }
    24. ListElement {
    25. text: qsTr("5.向左边滑动出现删除按钮")
    26. }
    27. }
    28. //列表控件
    29. ListView{
    30. id: listview
    31. width: parent.width
    32. height: parent.height
    33. anchors.fill: parent
    34. model: listModel
    35. delegate: listDelegate //列表项组件ID
    36. }
    37. //列表项组件
    38. Component{
    39. id: listDelegate
    40. Rectangle{
    41. id: listItem
    42. width: parent.width
    43. height: 60
    44. Text {
    45. id: text
    46. font.family: "microsoft yahei"
    47. font.pointSize: 18
    48. height: parent.height
    49. width: parent.width - delBtn.width
    50. text: model.text
    51. color: "green"
    52. verticalAlignment: Text.AlignVCenter
    53. anchors.left: parent.left
    54. anchors.leftMargin: 30
    55. MouseArea{
    56. property point clickPos: "0,0"
    57. anchors.fill: parent
    58. onPressed: {
    59. clickPos = Qt.point(mouse.x,mouse.y);//取鼠标坐标
    60. }
    61. onReleased: {
    62. var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)//释放鼠标后坐标
    63. if ((delta.x < 0) && (aBtnShow.running === false) && (delBtn.width === 0)){
    64. aBtnShow.start();
    65. }else if (aBtnHide.running === false && (delBtn.width > 0)){
    66. aBtnHide.start();
    67. }
    68. }
    69. }
    70. }
    71. Rectangle{
    72. color: "#AAAAAA"
    73. height: 1
    74. width: parent.width
    75. anchors.bottom: parent.bottom
    76. }
    77. Rectangle{
    78. id: delBtn
    79. height: parent.height
    80. color: "#EE4040"
    81. anchors.right: parent.right
    82. Text {
    83. font.family: "microsoft yahei"
    84. font.pointSize: 18
    85. anchors.centerIn: parent
    86. text: qsTr("删除")
    87. color: "#ffffff"
    88. }
    89. MouseArea{
    90. anchors.fill: parent
    91. onClicked: {
    92. listview.model.remove(index);
    93. }
    94. }
    95. }
    96. PropertyAnimation{
    97. id: aBtnShow
    98. target: delBtn
    99. property: "width"
    100. duration: 100
    101. from: 0
    102. to: 60
    103. }
    104. PropertyAnimation{
    105. id: aBtnHide
    106. target: delBtn
    107. property: "width"
    108. duration: 100
    109. from: 60
    110. to: 0
    111. }
    112. }
    113. }
    114. }

  • 相关阅读:
    小程序的深层了解
    React 学习笔记:JSX 语法
    ant的get任务
    vant3的option写法示例(部分组件:swipe、popup、picker、stepper、field、tab、tabbar)
    Privacy-preserving record linkage on large real world datasets论文总结
    Automated Testing for LLMOps 01:使用CircleCI进行持续集成CI
    【头歌】——抓取Ethernet包(计算机网络)
    基于未知环境下四旋飞行器运动规划应用研究(Matlab代码实现)
    前端面试基础面试题——10
    qt使用AES加密、解密字符串
  • 原文地址:https://blog.csdn.net/fittec/article/details/127660946