Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
12 : QFrame(parent)
13{
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.
28void DragWidget::dragEnterEvent(QDragEnterEvent *event)
29{
30 event->acceptProposedAction();
31}
33
36{
37 if (event->source() == this && event->possibleActions() & Qt::MoveAction)
38 return;
40
42 if (event->proposedAction() == Qt::MoveAction) {
43 event->acceptProposedAction();
44 // Process the data from the event.
46 emit dragResult(tr("The data was moved here."));
48 } else if (event->proposedAction() == Qt::CopyAction) {
49 event->acceptProposedAction();
50 // Process the data from the event.
52 emit dragResult(tr("The data was copied here."));
54 } else {
55 // Ignore the drop.
56 return;
57 }
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]));
65}
67
70{
71 if (event->button() == Qt::LeftButton)
72 dragStartPosition = event->pos();
73}
75
78{
79 if (!(event->buttons() & Qt::LeftButton))
80 return;
81 if ((event->pos() - dragStartPosition).manhattanLength()
83 return;
84
85 QDrag *drag = new QDrag(this);
87
88 mimeData->setData(mimeType, data);
89 drag->setMimeData(mimeData);
90
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 }
106}
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
117 formats << mimetype;
119}
void dragResult(const QString &actionText)
void mouseMoveEvent(QMouseEvent *event) override
[6]
void setData(const QString &mimetype, const QByteArray &newData)
[8]
void dropEvent(QDropEvent *event) override
[0]
void mimeTypes(const QStringList &types)
void dragEnterEvent(QDragEnterEvent *event) override
[0]
DragWidget(QWidget *parent=nullptr)
void mousePressEvent(QMouseEvent *event) override
[5]
int startDragDistance
the minimum distance required for a drag and drop operation to start.
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtGui
Definition qdrag.h:22
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Definition qdrag.cpp:201
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition qdrag.cpp:101
The QFrame class is the base class of widgets that can have a frame.
Definition qframe.h:17
@ Sunken
Definition qframe.h:51
void setFrameStyle(int)
Sets the frame style to style.
Definition qframe.cpp:300
@ StyledPanel
Definition qframe.h:45
The QHBoxLayout class lines up widgets horizontally.
Definition qboxlayout.h:78
The QLabel widget provides a text or image display.
Definition qlabel.h:20
void setText(const QString &)
Definition qlabel.cpp:263
void setAlignment(Qt::Alignment)
Definition qlabel.cpp:442
void addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout.
Definition qlayout.cpp:186
\inmodule QtCore
Definition qmimedata.h:16
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
\inmodule QtGui
Definition qevent.h:196
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QLayout * layout() const
Returns the layout manager that is installed on this widget, or \nullptr if no layout manager is inst...
void setAcceptDrops(bool on)
Definition qwidget.cpp:3436
EGLint EGLint * formats
@ AlignHCenter
Definition qnamespace.h:148
@ LeftButton
Definition qnamespace.h:58
DropAction
@ CopyAction
@ MoveAction
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
struct _cl_event * event
SSL_CTX int void * arg
#define tr(X)
#define emit
QMimeData * mimeData