Swipe to delete in QML

泄露秘密 提交于 2020-03-22 07:48:05

问题


Is it possible to delete items from a ListView in QML like on webOS, i.e. after swiping the entry there's "cancel" and "delete".

I'd like to use Qt 4.7 (so QtQuick 1.1)


回答1:


There is no default component in QtQuick that can handle gesture signals. There was a Qt labs project that introduced a GestureArea that may do what you want. It was not pacakged with QtQuick 1.1 and I am unsure as to its current status but feel free to give it a try. http://blog.qt.digia.com/blog/2010/10/05/getting-in-touch-with-qt-quick-gestures-and-qml/ Otherwise, there is no QML solution although Qt itself does have gesture programming support http://qt-project.org/doc/qt-4.7/gestures-overview.html




回答2:


There is a delete method in the help, see this example

  ListView {
    id: listView
    anchors.fill: parent
    model: ListModel {
        ListElement { sender: "Bob Bobbleton"; title: "How are you going?" }
        ListElement { sender: "Rug Emporium"; title: "SALE! All rugs MUST go!" }
        ListElement { sender: "Electric Co."; title: "Electricity bill 15/07/2016 overdue" }
        ListElement { sender: "Tips"; title: "Five ways this tip will save your life" }
    }
    delegate: SwipeDelegate {
        id: swipeDelegate
        text: model.sender + " - " + model.title
        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
            }
        }

        onClicked: {
            swipe.complete=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"
            }
        }
    }
}


来源:https://stackoverflow.com/questions/17860902/swipe-to-delete-in-qml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!