Qt Quick之ListView单元的上下拖动功能实现

       最近在项目中用到了ListView中单元的上下拖动功能,由于在网上找这方面的资料不是太多,所以这里将实现方法记录下来。实现方法:使用onMouseXChanged、onMouseYChanged两个函数来得到该区域内x,y变化后的坐标值,并在里面通过listview.indexAt()函数计算得到索引值index,最后使用move函数来移动listview显示单元。这里提供了一个简单的listview实例,并搭配上了移动时的动画效果,感觉还行。参考程序如下。

参考程序:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2


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: "三星公司"
        }
    }

    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: wrapper.ListView.isCurrentItem? "red":"black"
                    font.pixelSize: 20
                }
                Text {
                    text: cost
                    color: wrapper.ListView.isCurrentItem? "red":"black"
                    font.pixelSize: 20
                }
                Text {
                    text: manufacturer
                    color: wrapper.ListView.isCurrentItem? "red":"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)
                        }
                }
            }
        }

    }

    property int n_flag: 0
    ListView {
        id: listview
        anchors.fill: parent
        delegate: phone_delegate
        model:objmodel
        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
        width: 100
        height: 100
        border.width: 0.6
        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.log( listview.currentIndex )
                listview.move_down()
            }
        }

    }
}




要在Qt 5.15中使用QMLListView实现可通过触摸调节列宽的多列列表,你可以使用ListView的delegate属性和一个自定义的QML组件来实现。 首先,你需要创建一个自定义QML组件,用于显示每个列表项。在该组件中,你可以使用RowLayout或GridLayout来实现多列布局。然后,你可以在每个列的边缘添加一个可调节大小的控件,例如一个Slider或者一个MouseArea。 接下来,在ListView的delegate属性中,你可以使用这个自定义组件来显示每个列表项,同时使用ListView的model属性来指定列表数据。当用户拖动列宽调节控件时,你可以更新ListView中每个列表项的布局。 下面是一个简单的示例代码,展示了如何使用自定义组件实现可调节列宽的多列列表: ``` import QtQuick 2.0 // 自定义列表项组件 Item { id: listItem // 列表项中的多列布局 RowLayout { id: rowLayout // 第一列 Column { id: column1 width: 100 // 初始列宽 // 列宽调节控件 Slider { id: slider1 orientation: Qt.Vertical minimumValue: 50 maximumValue: 200 value: column1.width // 将Slider的值绑定到列的宽度上 onValueChanged: column1.width = value // 当Slider的值改变时,更新列的宽度 } // 列中的内容 Text { text: "Column 1" } } // 第二列 Column { id: column2 width: 150 // 初始列宽 // 列宽调节控件 Slider { id: slider2 orientation: Qt.Vertical minimumValue: 100 maximumValue: 300 value: column2.width // 将Slider的值绑定到列的宽度上 onValueChanged: column2.width = value // 当Slider的值改变时,更新列的宽度 } // 列中的内容 Text { text: "Column 2" } } } } // 使用ListView显示多列列表 ListView { id: listView width: 400 height: 400 model: ["Item 1", "Item 2", "Item 3"] // 列表数据 delegate: listItem // 自定义列表项组件 } ``` 在这段代码中,我们创建了一个自定义的QML组件,其中使用了RowLayout来实现多列布局。每个列中包含一个Slider控件和一个内容项,Slider控件用于调节列宽。在ListView的delegate属性中,我们使用这个自定义组件来显示每个列表项。当用户拖动Slider控件时,我们更新了每个列表项的布局,以实现可调节列宽的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术爱生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值