2025-10-29 17:23:30 +08:00
|
|
|
|
#include "qmytreewidget.h"
|
|
|
|
|
|
|
|
|
|
|
|
QMyTreeWidget::QMyTreeWidget(QWidget *parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
setDragEnabled(true);
|
|
|
|
|
|
setDefaultDropAction(Qt::MoveAction); // 或者 Qt::CopyAction,取决于你的需求
|
|
|
|
|
|
setAcceptDrops(true);
|
|
|
|
|
|
setDropIndicatorShown(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QMyTreeWidget::~QMyTreeWidget()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QMyTreeWidget::startDrag(Qt::DropActions supportedActions)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(currentItem()->parent()==NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
//分类节点不能拖动
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建拖拽事件
|
|
|
|
|
|
QTreeWidgetItem *item = currentItem();
|
|
|
|
|
|
QString strTreeTag = item->data(0, Qt::UserRole).toString();
|
|
|
|
|
|
if (strTreeTag == "root")
|
|
|
|
|
|
{
|
|
|
|
|
|
//根节点(项目名称)
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (strTreeTag == "curveObject"){
|
2025-11-18 11:40:42 +08:00
|
|
|
|
//曲线
|
2025-10-29 17:23:30 +08:00
|
|
|
|
QString strSlfName = item->data(0, Qt::UserRole+1).toString();
|
|
|
|
|
|
QString strWellName = item->data(0, Qt::UserRole+2).toString();
|
|
|
|
|
|
|
|
|
|
|
|
//曲线对象(AC、BS...)
|
|
|
|
|
|
QMimeData *mimeData = new QMimeData;
|
|
|
|
|
|
// 这里需要根据你的item数据来设置mimeData,例如:
|
|
|
|
|
|
mimeData->setText(strSlfName + "#@@#"+ strWellName + "#@@#" +item->text(0));
|
|
|
|
|
|
// 创建QDrag对象
|
|
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
|
|
// 可以设置拖拽时的光标图标
|
|
|
|
|
|
QRect itemRect = visualItemRect(item); // 获取项的矩形区域
|
|
|
|
|
|
QPixmap itemPixmap = QWidget::grab(itemRect);//QPixmap::grabWidget(this, itemRect); // 获取项的屏幕截图
|
|
|
|
|
|
// 调整拖拽光标的热点,使其位于截图的中心
|
|
|
|
|
|
drag->setPixmap(itemPixmap);
|
|
|
|
|
|
|
2025-11-18 11:40:42 +08:00
|
|
|
|
// 执行拖拽操作
|
|
|
|
|
|
Qt::DropAction dropAction = drag->exec(supportedActions, Qt::MoveAction);
|
|
|
|
|
|
/*if (dropAction == Qt::MoveAction) {
|
|
|
|
|
|
// 处理拖拽结束的逻辑,例如从界面上移除拖拽的项
|
|
|
|
|
|
for (QTreeWidgetItem *item : items) {
|
|
|
|
|
|
this->takeTopLevelItem(this->indexOfTopLevelItem(item));
|
|
|
|
|
|
}
|
|
|
|
|
|
}*/
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (strTreeTag == "waveObject"){
|
|
|
|
|
|
//波列数据
|
|
|
|
|
|
QString strSlfName = item->data(0, Qt::UserRole+1).toString();
|
|
|
|
|
|
QString strWellName = item->data(0, Qt::UserRole+2).toString();
|
|
|
|
|
|
|
|
|
|
|
|
//波列对象
|
|
|
|
|
|
QMimeData *mimeData = new QMimeData;
|
|
|
|
|
|
// 这里需要根据你的item数据来设置mimeData,例如:
|
|
|
|
|
|
mimeData->setText(strSlfName + "#$$#"+ strWellName + "#$$#" +item->text(0));
|
|
|
|
|
|
// 创建QDrag对象
|
|
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
|
|
// 可以设置拖拽时的光标图标
|
|
|
|
|
|
QRect itemRect = visualItemRect(item); // 获取项的矩形区域
|
|
|
|
|
|
QPixmap itemPixmap = QWidget::grab(itemRect);//QPixmap::grabWidget(this, itemRect); // 获取项的屏幕截图
|
|
|
|
|
|
// 调整拖拽光标的热点,使其位于截图的中心
|
|
|
|
|
|
drag->setPixmap(itemPixmap);
|
|
|
|
|
|
|
2025-10-29 17:23:30 +08:00
|
|
|
|
// 执行拖拽操作
|
|
|
|
|
|
Qt::DropAction dropAction = drag->exec(supportedActions, Qt::MoveAction);
|
|
|
|
|
|
/*if (dropAction == Qt::MoveAction) {
|
|
|
|
|
|
// 处理拖拽结束的逻辑,例如从界面上移除拖拽的项
|
|
|
|
|
|
for (QTreeWidgetItem *item : items) {
|
|
|
|
|
|
this->takeTopLevelItem(this->indexOfTopLevelItem(item));
|
|
|
|
|
|
}
|
|
|
|
|
|
}*/
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|