-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragdroparea.cpp
35 lines (30 loc) · 938 Bytes
/
dragdroparea.cpp
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
#include "dragdroparea.h"
#include <QDragEnterEvent>
#include <QLabel>
#include <QDebug>
DragDropArea::DragDropArea(QWidget *parent) :
QWidget(parent)
{
}
void DragDropArea::mousePressEvent(QMouseEvent *event)
{
QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
if (!child)
return;
// Only drag children with dynamic property: "drag"
if (!child->property("drag").toBool())
return;
QPoint hotSpot = event->pos() - child->pos();
QMimeData *mimeData = new QMimeData;
mimeData->setText(child->text());
mimeData->setData("application/x-hotspot",
QByteArray::number(hotSpot.x())
+ " " + QByteArray::number(hotSpot.y()));
QPixmap pixmap(child->size());
child->render(&pixmap);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot);
drag->exec();
}