Skip to content

Commit 4cbd82a

Browse files
committed
Add QmlListViewDragDrop
1 parent 7b1e20e commit 4cbd82a

File tree

7 files changed

+144
-0
lines changed

7 files changed

+144
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#-------------------------------------------------
2+
#
3+
# Copyright (C) 2003-2103 CamelSoft Corporation
4+
#
5+
#-------------------------------------------------
6+
7+
QT += qml quick
8+
9+
CONFIG += c++11
10+
11+
SOURCES += main.cpp
12+
13+
RESOURCES += qml.qrc

QmlListViewDragDrop/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*!
2+
*@file main.cpp
3+
*@brief 程序主文件
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
#include <QGuiApplication>
9+
#include <QQmlApplicationEngine>
10+
11+
int main(int argc, char *argv[])
12+
{
13+
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
14+
QGuiApplication app(argc, argv);
15+
16+
QQmlApplicationEngine engine;
17+
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
18+
if (engine.rootObjects().isEmpty())
19+
return -1;
20+
21+
return app.exec();
22+
}

QmlListViewDragDrop/main.qml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*!
2+
*@file main.qml
3+
*@brief 主文件
4+
*@version 1.0
5+
*@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6+
*@author zhengtianzuo
7+
*/
8+
import QtQuick 2.7
9+
import QtQuick.Controls 2.0
10+
11+
ApplicationWindow {
12+
visible: true
13+
width: 400
14+
height: 300
15+
title: qsTr("Qml列表项拖放")
16+
17+
ListModel {
18+
id: listModel
19+
20+
ListElement {
21+
text: qsTr("111222333随便的一些内容")
22+
}
23+
ListElement {
24+
text: qsTr("AAABBBCCC随便的一些内容")
25+
}
26+
ListElement {
27+
text: qsTr("DDDEEEFFF随便的一些内容")
28+
}
29+
ListElement {
30+
text: qsTr("GGGHHHIII随便的一些内容")
31+
}
32+
ListElement {
33+
text: qsTr("JJJKKKLLL随便的一些内容")
34+
}
35+
}
36+
37+
ListView{
38+
id: listview
39+
width: parent.width
40+
height: parent.height
41+
anchors.fill: parent
42+
model: listModel
43+
delegate: listDelegate
44+
interactive: false
45+
}
46+
47+
Component{
48+
id: listDelegate
49+
Rectangle{
50+
property int fromIndex: 0
51+
property int toIndex: 0
52+
53+
id: listItem
54+
width: parent.width
55+
height: 30
56+
57+
Text {
58+
id: label
59+
font.family: "microsoft yahei"
60+
font.pointSize: 12
61+
height: parent.height
62+
width: parent.width
63+
text: model.text
64+
color: "#148014"
65+
verticalAlignment: Text.AlignVCenter
66+
}
67+
Rectangle{
68+
color: "#AAAAAA"
69+
height: 1
70+
width: parent.width
71+
anchors.bottom: parent.bottom
72+
}
73+
MouseArea {
74+
id: mousearea
75+
anchors.fill: parent
76+
onPressed: {
77+
listview.currentIndex = index;
78+
listItem.fromIndex = index;
79+
label.color = "#4040FF"
80+
}
81+
onReleased: {
82+
label.color = "#148014"
83+
console.debug("fromIndex: ", listItem.fromIndex, "toIndex: ", listItem.toIndex);
84+
}
85+
onMouseYChanged: {
86+
var lastIndex = listview.indexAt(mousearea.mouseX + listItem.x,
87+
mousearea.mouseY + listItem.y);
88+
if ((lastIndex < 0) || (lastIndex > listModel.rowCount()))
89+
return;
90+
if (index !== lastIndex){
91+
listModel.move(index, lastIndex, 1);
92+
}
93+
listItem.toIndex = lastIndex;
94+
}
95+
}
96+
}
97+
}
98+
}

QmlListViewDragDrop/qml.qrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>main.qml</file>
4+
</qresource>
5+
</RCC>

QmlListViewDragDrop/show.gif

101 KB
Loading

QtQuickExamples.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ SUBDIRS += QmlListSlidDelete
4242
SUBDIRS += QmlCircularProgressButton
4343
SUBDIRS += QmlPageNavigation
4444
SUBDIRS += QmlUpDownRefresh
45+
SUBDIRS += QmlListViewDragDrop

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,8 @@ QmlLanguage: Qml动态语言切换
196196
QmlUpDownRefresh: Qml上拉下拉刷新
197197

198198
![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlUpDownRefresh/show.gif?raw=true)
199+
200+
QmlListViewDragDrop: Qml列表项拖放
201+
202+
![](https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlListViewDragDrop/show.gif?raw=true)
203+

0 commit comments

Comments
 (0)