Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
dragwidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtGui>
5#include <QLabel>
6#include <QHBoxLayout>
7#include <QApplication>
8
9#include "dragwidget.h"
10
11DragWidget::DragWidget(QWidget *parent)
12 : QFrame(parent)
13{
14 setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
15 dragDropLabel = new QLabel("", this);
16 dragDropLabel->setAlignment(Qt::AlignHCenter);
17
18 QHBoxLayout *layout = new QHBoxLayout(this);
19 layout->addStretch(0);
20 layout->addWidget(dragDropLabel);
21 layout->addStretch(0);
22
23 setAcceptDrops(true);
24}
25
26// Accept all actions, but deal with them separately later.
27//! [0]
28void DragWidget::dragEnterEvent(QDragEnterEvent *event)
29{
30 event->acceptProposedAction();
31}
32//! [0]
33
34//! [1]
35void DragWidget::dropEvent(QDropEvent *event)
36{
37 if (event->source() == this && event->possibleActions() & Qt::MoveAction)
38 return;
39//! [1]
40
41//! [2]
42 if (event->proposedAction() == Qt::MoveAction) {
43 event->acceptProposedAction();
44 // Process the data from the event.
45//! [2]
46 emit dragResult(tr("The data was moved here."));
47//! [3]
48 } else if (event->proposedAction() == Qt::CopyAction) {
49 event->acceptProposedAction();
50 // Process the data from the event.
51//! [3]
52 emit dragResult(tr("The data was copied here."));
53//! [4]
54 } else {
55 // Ignore the drop.
56 return;
57 }
58//! [4]
59 // End of quote
60
61 emit mimeTypes(event->mimeData()->formats());
62 setData(event->mimeData()->formats()[0],
63 event->mimeData()->data(event->mimeData()->formats()[0]));
64//! [5]
65}
66//! [5]
67
68//! [6]
69void DragWidget::mousePressEvent(QMouseEvent *event)
70{
71 if (event->button() == Qt::LeftButton)
72 dragStartPosition = event->pos();
73}
74//! [6]
75
76//! [7]
77void DragWidget::mouseMoveEvent(QMouseEvent *event)
78{
79 if (!(event->buttons() & Qt::LeftButton))
80 return;
81 if ((event->pos() - dragStartPosition).manhattanLength()
82 < QApplication::startDragDistance())
83 return;
84
85 QDrag *drag = new QDrag(this);
86 QMimeData *mimeData = new QMimeData;
87
88 mimeData->setData(mimeType, data);
89 drag->setMimeData(mimeData);
90
91 Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
92//! [7]
93
94 switch (dropAction) {
95 case Qt::CopyAction:
96 emit dragResult(tr("The text was copied."));
97 break;
98 case Qt::MoveAction:
99 emit dragResult(tr("The text was moved."));
100 break;
101 default:
102 emit dragResult(tr("Unknown action."));
103 break;
104 }
105//! [8]
106}
107//! [8]
108
109void DragWidget::setData(const QString &mimetype, const QByteArray &newData)
110{
111 mimeType = mimetype;
112 data = QByteArray(newData);
113
114 dragDropLabel->setText(tr("%1 bytes").arg(data.size()));
115
116 QStringList formats;
117 formats << mimetype;
118 emit mimeTypes(formats);
119}
void mouseMoveEvent(QMouseEvent *event) override
[6]
void setData(const QString &mimetype, const QByteArray &newData)
[8]
void dropEvent(QDropEvent *event) override
[0]
void dragEnterEvent(QDragEnterEvent *event) override
[0]
void mousePressEvent(QMouseEvent *event) override
[5]