2025-05-09 19:36:32 +08:00
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
#include <QMimeData>
|
|
|
|
|
|
#include <QDrag>
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
#include "topologyTree.h"
|
|
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
|
|
|
|
TopologyTree::TopologyTree(QWidget *parent)
|
|
|
|
|
|
: QTreeView(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TopologyTree::~TopologyTree()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TopologyTree::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (event->buttons() & Qt::LeftButton) {
|
|
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
|
|
|
|
|
if (index.isValid()) {
|
|
|
|
|
|
// 创建QMimeData,并将选中项的文本放入
|
|
|
|
|
|
QMimeData *mimeData = new QMimeData();
|
|
|
|
|
|
mimeData->setText(model()->data(index, Qt::DisplayRole).toString());
|
2025-05-16 19:20:46 +08:00
|
|
|
|
QString id = index.data(Qt::UserRole).toString();
|
|
|
|
|
|
mimeData->setData("application/id",id.toLocal8Bit());
|
2025-05-09 19:36:32 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建拖拽对象
|
|
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置拖拽的图标(这里用一个简单的图标,也可以使用其他方式)
|
|
|
|
|
|
QPixmap pixmap(100, 30);
|
|
|
|
|
|
pixmap.fill(Qt::white);
|
|
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
|
painter.drawText(0, 20, model()->data(index, Qt::DisplayRole).toString());
|
|
|
|
|
|
drag->setPixmap(pixmap);
|
|
|
|
|
|
drag->setHotSpot(event->pos() - rect().topLeft());
|
|
|
|
|
|
|
|
|
|
|
|
// 启动拖拽操作
|
|
|
|
|
|
drag->exec(Qt::CopyAction);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|