DiagramDesigner/source/topologyTree.cpp

47 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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());
QString id = index.data(Qt::UserRole).toString();
mimeData->setData("application/id",id.toLocal8Bit());
// 创建拖拽对象
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);
}
}
}