Search code examples
androidiosqtqmlqtquickcontrols2

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. When I try to "swipe right" and then tap the remove button, the "Listview-item" will not be deleted. 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"
                }
            }
        }
    }
}

Solution

  • You can add a MouseArea with an onClicked-event inside the Rectangle. 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)
                    }
    }