listview 拖拽

import QtQuick 2.12
import QtQuick.Window 2.12

//演示ListView的拖拽移动
Window {
    width: 360
    height: 520
    visible: true
    title: qsTr("ListView Move")



    Rectangle {
        width: 800
        height: 600

        ListModel {
            id: objmodel
            ListElement{
                name: "苹果"
                cost: "5000"
                manufacturer: "Apple公司"
            }
            ListElement{
                name: "小米2"
                cost: "2000"
                manufacturer: "小米公司"
            }
            ListElement{
                name: "三星"
                cost: "3000"
                manufacturer: "三星公司"
            }
        }

        property int first_index: -1
        property int last_index: -1

        Component {
            id: phone_delegate

            Item {
                id: wrapper
                width: parent.width
                height: 30

                Row {
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    spacing: 8
                    Text {
                        id: coll
                        text: name
                        color: "black"
                        font.pixelSize: 20
                    }
                    Text {
                        text: cost
                        color: "black"
                        font.pixelSize: 20
                    }
                    Text {
                        text: manufacturer
                        color: "black"
                        font.pixelSize: 20
                    }

                }

                MouseArea {
                    id: mousearea

                    anchors.fill: parent
                    onClicked: {

                    }
                    onPressed: {

                    }
                    onReleased: {

                    }

                    onMouseXChanged: {
                        var pore = listview.indexAt(mousearea.mouseX + wrapper.x, mousearea.mouseY + wrapper.y)


                        if(index !== pore)
                        {

                            objmodel.move(index, pore, 1)
                        }
                    }
                    onMouseYChanged: {

                        var pore = listview.indexAt(mousearea.mouseX + wrapper.x, mousearea.mouseY + wrapper.y)


                        if(index !== pore)
                        {

                            objmodel.move(index, pore, 1)
                        }
                    }
                }
            }
        }

        ListView {
            id: listview

            anchors.left: parent.left
            width: 300
            height: 300
            delegate: phone_delegate
            model: objmodel

            interactive: false
            focus: true

            move: Transition {
                NumberAnimation { properties: "x,y"; duration: 100 }
            }
        }

        ListModel {

            id: second_model
            ListElement {
                name: "苹果"
                cost: "5000"
                manufacturer: "Apple公司"
            }
            ListElement {
                name: "小米2"
                cost: "2000"
                manufacturer: "小米公司"
            }
            ListElement {
                name: "三星"
                cost: "3000"
                manufacturer: "三星公司"
            }
        }
        Component {
            id: android_delegate

            Item {
                id: second_wrapper

                width: parent.width
                height: 30

                Row {
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    spacing: 8
                    Text {
                        id: coll
                        text: name
                        color: "black"
                        font.pixelSize: 20
                    }
                    Text {
                        text: cost
                        color: "black"
                        font.pixelSize: 20
                    }
                    Text {
                        text: manufacturer
                        color: "black"
                        font.pixelSize: 20
                    }


                }

                Text {
                    id: txt
                    text:
                        second_model.get( index ).name + " "
                        + second_model.get( index ).cost + " "
                        + second_model.get( index ).manufacturer
                    visible: false
                }

                MouseArea {
                    id: mousearea
                    anchors.fill: parent
                    drag.target: txt
                    drag.axis: Drag.XAndYAxis
                    drag.minimumX: mouseX
                    drag.maximumX: 0
                    drag.minimumY: mouseY
                    drag.maximumY: second_wrapper.height

                    onClicked: {

                    }
                    onPressed: {
//                        pre_index = second_view.currentIndex


                    }
                    onReleased: {

                    }

                    onMouseXChanged: {

                    }
                    onMouseYChanged: {

                    }
                }
            }

        }

        property int n_flag: 0

        ListView {
            id: second_view

            anchors.left: listview.right
            width: 300
            height: 300
            delegate: android_delegate
            model: second_model

            interactive: false
            focus: true
            function move_down() {

                if( ( n_flag == 0 ) && ( currentIndex+1 ) < second_model.count ) {
                    model.move( currentIndex, currentIndex+1, 1)
                }
                if( n_flag == 1 && ( currentIndex-1 ) >= 0) {
                    model.move( currentIndex, currentIndex-1, 1)
                }
                if( currentIndex -1 == 0 ) {
                    n_flag = 0;
                }
                if( currentIndex + 1 == second_model.count ) {
                    n_flag = 1
                }

            }

            move: Transition {
                NumberAnimation {properties: "x,y"; duration: 100;}
            }
        }

        Rectangle {
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            width: 100
            height: 100
            border.width: 0.6
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log( second_view.currentIndex )
                    second_view.move_down()
                }
            }

        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值