QML中鼠标拖动移动ListView中项的位置
来源:互联网 发布:小米笔记本装mac系统 编辑:程序博客网 时间:2024/06/03 19:26
在QML开发中,ListView是我们经常用到的控件,可以用它给用户展示出列表,但是往往都是将项目的显示顺序排好后,直接让ListView显示出来,亦或者是知道要移动到具体的那一位置,然后调整数据在ListView中的顺序来达到要求,现有一种需求,就是用鼠标拖动某项,动态去改变某一项在ListView中显示的顺序位置,经过研究及实践实战,实现方式的核心代码如下:
import QtQuick 2.4import QtQuick.Controls 1.3import QtQuick.Window 2.2import QtQuick.Dialogs 1.2Rectangle { width: 800 height: 600 ListModel{ id: objmodelListElement{ name: "小米" cost: "2500" manufacturer: "小米公司" } ListElement{ name: "苹果" cost: "5000" manufacturer: "Apple公司" } ListElement{ name: "小米2" cost: "2000" manufacturer: "小米公司" } ListElement{ name: "三星" cost: "3000" manufacturer: "三星公司" }ListElement{ name: "华为" cost: "3000" manufacturer: "华为公司" }ListElement{ name: "ViVo" cost: "3000" manufacturer: "ViVo公司" } } Component { id: com_delegate Item { id: wrapper width: parent.width height: 30Rectangle {id: bgColoranchors.fill: parentcolor: "transparent"} 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: {listview.currentIndex = index; } onPressed: { bgColor.color = "blue" } onReleased: {bgColor.color = "transparent" } 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: listviewanchors.centerIn:parentwidth: 300height: 300 delegate: com_delegate model:objmodel interactive: false focus: true function move_down() { if( ( n_flag == 0 ) && ( currentIndex+1 ) < 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 == 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() } } }}鼠标拖动某项到指定的ListView中的某位置,然后放开鼠标,该项就移动到指定位置了。
左下角的框代表一个按钮(虽然丑了点,说明问题就行^__^),点击可以改变项的显示位置。
具体的效果图自己脑补,是在不行,直接拷贝以上代码,用qmlscene运行,就可看到效果了。
阅读全文
1 0
- QML中鼠标拖动移动ListView中项的位置
- 我的Unity(2)一点一滴 鼠标拖动物体,物体在三维中移动到该位置。
- VC中随鼠标移动显示鼠标的位置
- Unity3d中使用鼠标拖动物体移动
- Unity2D中使用鼠标拖动物体移动
- winform 鼠标拖动移动图片位置
- JQuery实现鼠标拖动元素移动位置
- c# WPF 中图片缩放功能,鼠标拖动位置
- QML中FRAMELESS 和 transparent 窗体的拖动实现
- QML中FRAMELESS 和 transparent 窗体的拖动实现
- Android中ListView组件的Item拖动
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- 在运行时通过鼠标拖动移动控件位置及改变控件的大小
- Unity中物体如何移动到鼠标点击位置
- 各个浏览器中鼠标位置的属性 offsetX layerX PageX clientX (自由拖动框,防止文字选中)
- 在运行时通过鼠标拖动移动控件位置(c#)
- 在运行时通过鼠标拖动移动控件位置(c#)
- 前端性能优化之图片优化方法
- DIV在100%下iframe宽度高度的自适应的问题
- 洛谷 P1074 靶形数独
- os_time.c
- 前端性能优化之视频优化方法
- QML中鼠标拖动移动ListView中项的位置
- Ubuntu16.04自带Python2.7和Python3,为什么用pip安装的包在Python3中不能用
- JAVA注解说明及应用实例
- 【leetcode】20. Valid Parentheses(Python & C++)
- BMP格式-dephi
- Nginx+Tomcat搭建负载均衡
- 天地图按照官方教程闪退的问题
- jQuery选择器
- @ModelAttribute