#include "TestMouseWidget.h"
#include "ui_TestMouseWidget.h"
#include <QtWidgets>
#include <QMouseEvent>
#include <QPushButton>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDrag>
#include <QDebug>
#include "TestButton.h"
TestMouseWidget::TestMouseWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TestMouseWidget)
{
ui->setupUi(this);
setAcceptDrops(true);
m_hash.clear();
}
TestMouseWidget::~TestMouseWidget()
{
delete ui;
}
/**
*@brief 用户放开了鼠标完成drop操作。一般来说,应该解析出该drag所携带的data,
* 然后做一个插入操作。调用acceptProposedAction()来通知源窗口,说此操作已经被接受,
* 源窗口应于此时做一个删除/复制操作。
*/
void TestMouseWidget::dropEvent(QDropEvent *e)
{
//qDebug()<<"target dropEvent :"<<e->mimeData()->text();
e->acceptProposedAction();
TestButton *pBtn = nullptr;
if(m_hash.contains(e->mimeData()->text()))
{
pBtn = m_hash.value(e->mimeData()->text());
QPoint hotSpot;
QByteArrayList hotSpotPos = e->mimeData()->data(QStringLiteral("application/x-hotspot")).split(' ');
if (hotSpotPos.size() == 2) {
hotSpot.setX(hotSpotPos.first().toInt());
hotSpot.setY(hotSpotPos.last().toInt());
}
pBtn->move(e->pos()-hotSpot);
}
else
{
pBtn = new TestButton(this);
pBtn->setText(e->mimeData()->text());
pBtn->resize(80,20);
m_hash.insert(pBtn->text(),pBtn);
pBtn->move(e->pos());
}
pBtn->show();
}
/**
*@brief 表示拖动操作进入本窗口,如果该event被ignore,
* 则鼠标在窗口中会显示一个forbidden状态的圆圈,表示禁止拖进来
*/
void TestMouseWidget::dragEnterEvent(QDragEnterEvent *e)
{
qDebug()<<"target dragEnterEvent :"<<e->mimeData()->text();
e->acceptProposedAction();
}
/**
*@brief 用户拖动东西在目标窗口内游荡,在拖到某此点位时,应提示允许其drop放下。
* 比如目标窗口里有若干个坑,那么当鼠标over到坑上时,就应该提示这里可以drop并改
* 变鼠标形状。
*/
void TestMouseWidget::dragMoveEvent(QDragMoveEvent *e)
{
//qDebug()<<"target dragMoveEvent :"<<e->mimeData()->text();
e->acceptProposedAction();
}
Qt控件拖拽最简实践
5星 · 超过95%的资源 需积分: 1 174 浏览量
2018-11-28
10:35:52
上传
评论 12
收藏 7KB ZIP 举报
唐麦片
- 粉丝: 67
- 资源: 16
最新资源
- 基于CSS的响应式鲜花网站全屏效果设计源码
- 基于JavaScript的访客预约系统设计源码
- 基于Vue和ECharts的工作租房数据可视化系统设计源码
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
- 基于Node.js的Express框架与MySQL的后台管理系统设计源码
- 基于Java的Flink流批一体数据处理快速集成开发框架设计源码
- FirstFilterOrderCompare
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈