简体   繁体   English

Qt / QML SwipeDelegate在移动设备(Android,iOS)上无法正常工作

[英]Qt/QML SwipeDelegate doesn't work properly on mobile devices (Android, iOS)

I'm new to Qt/QML programming and am trying to get the following example to run properly on a mobile device. 我是Qt / QML编程的新手,正在尝试使以下示例在移动设备上正常运行。 When I try to "swipe right" and then tap the remove button, the "Listview-item" will not be deleted. 当我尝试“向右滑动”,然后点击“删除”按钮时,“ Listview-item”将不会被删除。 On Desktop all works fine, but on a mobile device it doesn't work properly. 在台式机上一切正常,但在移动设备上无法正常工作。 Can anyone help me with my problem? 谁能帮我解决我的问题?

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    id: appWindow
    visible: true

    ListView {
        id: listView
        anchors.fill: parent
        model: ListModel {
            ListElement { name: "Swipe Delegate - Test 1" }
            ListElement { name: "Swipe Delegate - Test 2" }
            ListElement { name: "Swipe Delegate - Test 3" }
            ListElement { name: "Swipe Delegate - Test 4" }
        }
        delegate: SwipeDelegate {
            id: swipeDelegate
            text: model.name
            width: parent.width

            ListView.onRemove: SequentialAnimation {
                PropertyAction {
                    target: swipeDelegate
                    property: "ListView.delayRemove"
                    value: true
                }
                NumberAnimation {
                    target: swipeDelegate
                    property: "height"
                    to: 0
                    easing.type: Easing.InOutQuad
                }
                PropertyAction {
                    target: swipeDelegate;
                    property: "ListView.delayRemove";
                    value: false
                }
            }

            swipe.right: Label {
                id: deleteLabel
                text: qsTr("Delete")
                color: "white"
                verticalAlignment: Label.AlignVCenter
                padding: 12
                height: parent.height
                anchors.right: parent.right

                SwipeDelegate.onClicked: listView.model.remove(index)

                background: Rectangle {
                    color: deleteLabel.SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato"
                }
            }
        }
    }
}

You can add a MouseArea with an onClicked-event inside the Rectangle. 您可以在Rectangle中添加带有onClicked事件的MouseArea。 Here is the example: 这是示例:

 background: Rectangle {
                color: deleteLabel.SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato"
                MouseArea {
                    anchors.fill: parent
                    onClicked: listView.model.remove(index)
                }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM