拖拽事件流程

  1. mousePressEvent 鼠标按下
  2. mouseMoveEvent 鼠标开始移动(接下来换成dragMoveEvent)
  3. dragEnterEvent 开始拖拽
  4. dragMoveEvent
  5. dragMoveEvent
  6. …(重复)
  7. dragMoveEvent
  8. dragMoveEvent
  9. dropEvent 鼠标松开的时候触发(拖到外面不会触发)

拖拽没有 mouseReleaseEvent,只在点击时有

如果拖到 QListView 的底部空白,根据点获取到的索引是 -1

头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef DRAG_TABLE_H
#define DRAG_TABLE_H

#include <QTableWidget>
#include <QListWidget>
#include <QtGui>
#include <QDrag>

class DragListWidget : public QListWidget
{
public:
DragListWidget(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
public:
QPoint startPos;
QPoint endPos;
};

#endif

源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "DragTable.h"

DragListWidget::DragListWidget(QWidget *parent):QListWidget(parent)
{
this->setCurrentRow(10);
addItem("1");
addItem("2");
addItem("3");
addItem("4");
addItem("5");
addItem("6");
addItem("7");
addItem("8");
addItem("9");
addItem("10");
this->setAcceptDrops(true);//设置QListWidget的Item项可以被拖拽
}

void DragListWidget::mousePressEvent( QMouseEvent *event )
{
if (event->button() == Qt::LeftButton)
startPos = event->pos();
QListWidget::mousePressEvent(event);
}

void DragListWidget::mouseMoveEvent(QMouseEvent *event)
{
QListWidgetItem *item = currentItem();
mimeData = new QMimeData;
mimeData->setText(item->text());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
//drag->setPixmap(pixmap);
Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
if(dropAction == Qt::MoveAction)
{
delete item;//删除原有的item,在dropEvent(QDropEvent *event)函数中插入item
}
}

void DragListWidget::mouseReleaseEvent(QMouseEvent *event)
{
int distance = (endPos - startPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
{
startDrag();
}
}

void DragListWidget::dragEnterEvent( QDragEnterEvent *event )
{
DragListWidget *source = (DragListWidget *)((void*)(event->source()));
if (source && source == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
}

void DragListWidget::dragMoveEvent( QDragMoveEvent *event )
{
DragListWidget *source = (DragListWidget *)((void*)(event->source()));
if (source && source == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
}

void DragListWidget::dropEvent( QDropEvent *event )
{
DragListWidget *source = (DragListWidget *)((void*)(event->source()));
if (source && source == this)
{
endPos = event->pos();//得到鼠标移动到的坐标
QListWidgetItem *itemRow = itemAt(endPos);.//通过endPos获取鼠标位置所在的行
int rowCount = row(itemRow);
insertItem(rowCount + 1,mimeData->text());//row+1 为鼠标所指向的行,如果只是向末尾位置插入,把insertItem(rowCount + 1,mimeData->text())改为addItem(mimeData->text())
event->setDropAction(Qt::MoveAction);
event->accept();
}
}

博客来源:https://blog.csdn.net/yueye30121/article/details/12508675