-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomtableview.cpp
More file actions
60 lines (48 loc) · 1.82 KB
/
customtableview.cpp
File metadata and controls
60 lines (48 loc) · 1.82 KB
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
#include "customtableview.h"
CustomTableView::CustomTableView(QWidget *parent) : QTableView(parent) {}
void CustomTableView::dropEvent(QDropEvent *event) {
QModelIndex targetIndex = indexAt(event->position().toPoint());
if (!targetIndex.isValid()) {
return;
}
const QMimeData *mimeData = event->mimeData();
if (!mimeData->hasFormat("application/x-qabstractitemmodeldatalist")) {
return;
}
QByteArray itemData = mimeData->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&itemData, QIODevice::ReadOnly);
int sourceRow, col;
QMap<int, QVariant> roleDataMap;
stream >> sourceRow >> col >> roleDataMap;
QStandardItemModel *model = qobject_cast<QStandardItemModel *>(this->model());
if (!model || sourceRow < 0 || sourceRow >= model->rowCount()) {
return;
}
int targetRow = targetIndex.row();
// 先备份被移动的行数据
QList<QStandardItem*> sourceItems;
for (int i = 0; i < model->columnCount(); ++i) {
QStandardItem *item = model->item(sourceRow, i);
sourceItems.append(item ? item->clone() : nullptr);
}
// 计算插入行的位置
int newRow;
if (sourceRow < targetRow) {
// 从上向下拖动时,插入到目标行下方
newRow = targetRow + 1;
model->insertRow(newRow);
for (int i = 0; i < sourceItems.size(); ++i) {
model->setItem(newRow, i, sourceItems[i]);
}
model->removeRow(sourceRow);
} else {
// 从下向上拖动时,插入到目标行上方
newRow = targetRow;
model->insertRow(newRow);
for (int i = 0; i < sourceItems.size(); ++i) {
model->setItem(newRow, i, sourceItems[i]);
}
model->removeRow(sourceRow + 1);
}
event->acceptProposedAction();
}