Skip to content

Commit

Permalink
Add QmlListViewDragDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengtianzuo committed Nov 25, 2017
1 parent 7b1e20e commit 4cbd82a
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
13 changes: 13 additions & 0 deletions QmlListViewDragDrop/QmlListViewDragDrop.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#-------------------------------------------------
#
# Copyright (C) 2003-2103 CamelSoft Corporation
#
#-------------------------------------------------

QT += qml quick

CONFIG += c++11

SOURCES += main.cpp

RESOURCES += qml.qrc
22 changes: 22 additions & 0 deletions QmlListViewDragDrop/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*!
*@file main.cpp
*@brief 程序主文件
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

return app.exec();
}
98 changes: 98 additions & 0 deletions QmlListViewDragDrop/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*!
*@file main.qml
*@brief 主文件
*@version 1.0
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
*@author zhengtianzuo
*/
import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
visible: true
width: 400
height: 300
title: qsTr("Qml列表项拖放")

ListModel {
id: listModel

ListElement {
text: qsTr("111222333随便的一些内容")
}
ListElement {
text: qsTr("AAABBBCCC随便的一些内容")
}
ListElement {
text: qsTr("DDDEEEFFF随便的一些内容")
}
ListElement {
text: qsTr("GGGHHHIII随便的一些内容")
}
ListElement {
text: qsTr("JJJKKKLLL随便的一些内容")
}
}

ListView{
id: listview
width: parent.width
height: parent.height
anchors.fill: parent
model: listModel
delegate: listDelegate
interactive: false
}

Component{
id: listDelegate
Rectangle{
property int fromIndex: 0
property int toIndex: 0

id: listItem
width: parent.width
height: 30

Text {
id: label
font.family: "microsoft yahei"
font.pointSize: 12
height: parent.height
width: parent.width
text: model.text
color: "#148014"
verticalAlignment: Text.AlignVCenter
}
Rectangle{
color: "#AAAAAA"
height: 1
width: parent.width
anchors.bottom: parent.bottom
}
MouseArea {
id: mousearea
anchors.fill: parent
onPressed: {
listview.currentIndex = index;
listItem.fromIndex = index;
label.color = "#4040FF"
}
onReleased: {
label.color = "#148014"
console.debug("fromIndex: ", listItem.fromIndex, "toIndex: ", listItem.toIndex);
}
onMouseYChanged: {
var lastIndex = listview.indexAt(mousearea.mouseX + listItem.x,
mousearea.mouseY + listItem.y);
if ((lastIndex < 0) || (lastIndex > listModel.rowCount()))
return;
if (index !== lastIndex){
listModel.move(index, lastIndex, 1);
}
listItem.toIndex = lastIndex;
}
}
}
}
}
5 changes: 5 additions & 0 deletions QmlListViewDragDrop/qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Binary file added QmlListViewDragDrop/show.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions QtQuickExamples.pro
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ SUBDIRS += QmlListSlidDelete
SUBDIRS += QmlCircularProgressButton
SUBDIRS += QmlPageNavigation
SUBDIRS += QmlUpDownRefresh
SUBDIRS += QmlListViewDragDrop
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ QmlLanguage: Qml动态语言切换
QmlUpDownRefresh: Qml上拉下拉刷新

![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlUpDownRefresh/show.gif?raw=true)

QmlListViewDragDrop: Qml列表项拖放

![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlListViewDragDrop/show.gif?raw=true)

0 comments on commit 4cbd82a

Please sign in to comment.