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
qdrag.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <qdrag.h>
6#include "private/qguiapplication_p.h"
7#include "qpa/qplatformintegration.h"
8#include "qpa/qplatformdrag.h"
9#include <qpixmap.h>
10#include <qpoint.h>
11#include "qdnd_p.h"
12
13#include <QtCore/qpointer.h>
14
16
17/*!
18 \class QDrag
19 \inmodule QtGui
20 \ingroup draganddrop
21 \brief The QDrag class provides support for MIME-based drag and drop data
22 transfer.
23
24 Drag and drop is an intuitive way for users to copy or move data around in an
25 application, and is used in many desktop environments as a mechanism for copying
26 data between applications. Drag and drop support in Qt is centered around the
27 QDrag class that handles most of the details of a drag and drop operation.
28
29 The data to be transferred by the drag and drop operation is contained in a
30 QMimeData object. This is specified with the setMimeData() function in the
31 following way:
32
33 \snippet dragging/mainwindow.cpp 1
34
35 Note that setMimeData() assigns ownership of the QMimeData object to the
36 QDrag object. The QDrag must be constructed on the heap with a parent QObject
37 to ensure that Qt can clean up after the drag and drop operation has been
38 completed.
39
40 A pixmap can be used to represent the data while the drag is in
41 progress, and will move with the cursor to the drop target. This
42 pixmap typically shows an icon that represents the MIME type of
43 the data being transferred, but any pixmap can be set with
44 setPixmap(). The cursor's hot spot can be given a position
45 relative to the top-left corner of the pixmap with the
46 setHotSpot() function. The following code positions the pixmap so
47 that the cursor's hot spot points to the center of its bottom
48 edge:
49
50 \snippet separations/finalwidget.cpp 2
51
52 \note On X11, the pixmap may not be able to keep up with the mouse
53 movements if the hot spot causes the pixmap to be displayed
54 directly under the cursor.
55
56 The source and target widgets can be found with source() and target().
57 These functions are often used to determine whether drag and drop operations
58 started and finished at the same widget, so that special behavior can be
59 implemented.
60
61 QDrag only deals with the drag and drop operation itself. It is up to the
62 developer to decide when a drag operation begins, and how a QDrag object should
63 be constructed and used. For a given widget, it is often necessary to
64 reimplement \l{QWidget::mousePressEvent()}{mousePressEvent()} to determine
65 whether the user has pressed a mouse button, and reimplement
66 \l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} to check whether a QDrag is
67 required.
68
69 \sa {Drag and Drop}, QClipboard, QMimeData, {Draggable Icons Example},
70 {Draggable Text Example}, {Drop Site Example}
71*/
72
73/*!
74 Constructs a new drag object for the widget specified by \a dragSource.
75*/
76QDrag::QDrag(QObject *dragSource)
77 : QObject(*new QDragPrivate, dragSource)
78{
79 Q_D(QDrag);
80 d->source = dragSource;
81 d->target = nullptr;
82 d->data = nullptr;
83 d->hotspot = QPoint(-10, -10);
84 d->executed_action = Qt::IgnoreAction;
85 d->supported_actions = Qt::IgnoreAction;
86 d->default_action = Qt::IgnoreAction;
87}
88
89/*!
90 Destroys the drag object.
91*/
92QDrag::~QDrag()
93{
94 Q_D(QDrag);
95 delete d->data;
96}
97
98/*!
99 Sets the data to be sent to the given MIME \a data. Ownership of the data is
100 transferred to the QDrag object.
101*/
102void QDrag::setMimeData(QMimeData *data)
103{
104 Q_D(QDrag);
105 if (d->data == data)
106 return;
107 if (d->data != nullptr)
108 delete d->data;
109 d->data = data;
110}
111
112/*!
113 Returns the MIME data that is encapsulated by the drag object.
114*/
115QMimeData *QDrag::mimeData() const
116{
117 Q_D(const QDrag);
118 return d->data;
119}
120
121/*!
122 Sets \a pixmap as the pixmap used to represent the data in a drag
123 and drop operation. You can only set a pixmap before the drag is
124 started.
125*/
126void QDrag::setPixmap(const QPixmap &pixmap)
127{
128 Q_D(QDrag);
129 d->pixmap = pixmap;
130}
131
132/*!
133 Returns the pixmap used to represent the data in a drag and drop operation.
134*/
135QPixmap QDrag::pixmap() const
136{
137 Q_D(const QDrag);
138 return d->pixmap;
139}
140
141/*!
142 Sets the position of the hot spot relative to the top-left corner of the
143 pixmap used to the point specified by \a hotspot.
144
145 \b{Note:} on X11, the pixmap may not be able to keep up with the mouse
146 movements if the hot spot causes the pixmap to be displayed
147 directly under the cursor.
148*/
149void QDrag::setHotSpot(const QPoint& hotspot)
150{
151 Q_D(QDrag);
152 d->hotspot = hotspot;
153}
154
155/*!
156 Returns the position of the hot spot relative to the top-left corner of the
157 cursor.
158*/
159QPoint QDrag::hotSpot() const
160{
161 Q_D(const QDrag);
162 return d->hotspot;
163}
164
165/*!
166 Returns the source of the drag object. This is the widget where the drag
167 and drop operation originated.
168*/
169QObject *QDrag::source() const
170{
171 Q_D(const QDrag);
172 return d->source;
173}
174
175/*!
176 Returns the target of the drag and drop operation. This is the widget where
177 the drag object was dropped.
178*/
179QObject *QDrag::target() const
180{
181 Q_D(const QDrag);
182 return d->target;
183}
184
185/*!
186 \since 4.3
187
188 Starts the drag and drop operation and returns a value indicating the requested
189 drop action when it is completed. The drop actions that the user can choose
190 from are specified in \a supportedActions. The default proposed action will be selected
191 among the allowed actions in the following order: Move, Copy and Link.
192
193 \b{Note:} On Linux and \macos, the drag and drop operation
194 can take some time, but this function does not block the event
195 loop. Other events are still delivered to the application while
196 the operation is performed. On Windows, the Qt event loop is
197 blocked during the operation.
198
199 \sa cancel()
200*/
201
202Qt::DropAction QDrag::exec(Qt::DropActions supportedActions)
203{
204 return exec(supportedActions, Qt::IgnoreAction);
205}
206
207/*!
208 \since 4.3
209
210 Starts the drag and drop operation and returns a value indicating the requested
211 drop action when it is completed. The drop actions that the user can choose
212 from are specified in \a supportedActions.
213
214 The \a defaultDropAction determines which action will be proposed when the user performs a
215 drag without using modifier keys.
216
217 \b{Note:} On Linux and \macos, the drag and drop operation
218 can take some time, but this function does not block the event
219 loop. Other events are still delivered to the application while
220 the operation is performed. On Windows, the Qt event loop is
221 blocked during the operation. However, QDrag::exec() on
222 Windows causes processEvents() to be called frequently to keep the GUI responsive.
223 If any loops or operations are called while a drag operation is active, it will block the drag operation.
224*/
225
226Qt::DropAction QDrag::exec(Qt::DropActions supportedActions, Qt::DropAction defaultDropAction)
227{
228 Q_D(QDrag);
229 if (!d->data) {
230 qWarning("QDrag: No mimedata set before starting the drag");
231 return d->executed_action;
232 }
233 Qt::DropAction transformedDefaultDropAction = Qt::IgnoreAction;
234
235 if (defaultDropAction == Qt::IgnoreAction) {
236 if (supportedActions & Qt::MoveAction) {
237 transformedDefaultDropAction = Qt::MoveAction;
238 } else if (supportedActions & Qt::CopyAction) {
239 transformedDefaultDropAction = Qt::CopyAction;
240 } else if (supportedActions & Qt::LinkAction) {
241 transformedDefaultDropAction = Qt::LinkAction;
242 }
243 } else {
244 transformedDefaultDropAction = defaultDropAction;
245 }
246 d->supported_actions = supportedActions;
247 d->default_action = transformedDefaultDropAction;
248 QPointer<QDrag> self = this;
249 auto executed_action = QDragManager::self()->drag(self.data());
250 if (self.isNull())
251 return Qt::IgnoreAction;
252 d->executed_action = executed_action;
253 return d->executed_action;
254}
255
256/*!
257 Sets the drag \a cursor for the \a action. This allows you
258 to override the default native cursors. To revert to using the
259 native cursor for \a action pass in a null QPixmap as \a cursor.
260
261 Note: setting the drag cursor for IgnoreAction may not work on
262 all platforms. X11 and macOS has been tested to work. Windows
263 does not support it.
264*/
265void QDrag::setDragCursor(const QPixmap &cursor, Qt::DropAction action)
266{
267 Q_D(QDrag);
268 if (cursor.isNull())
269 d->customCursors.remove(action);
270 else
271 d->customCursors[action] = cursor;
272}
273
274/*!
275 Returns the drag cursor for the \a action.
276
277 \since 5.0
278*/
279
280QPixmap QDrag::dragCursor(Qt::DropAction action) const
281{
282 typedef QMap<Qt::DropAction, QPixmap>::const_iterator Iterator;
283
284 Q_D(const QDrag);
285 const Iterator it = d->customCursors.constFind(action);
286 if (it != d->customCursors.constEnd())
287 return it.value();
288
289 Qt::CursorShape shape = Qt::ForbiddenCursor;
290 switch (action) {
291 case Qt::MoveAction:
292 shape = Qt::DragMoveCursor;
293 break;
294 case Qt::CopyAction:
295 shape = Qt::DragCopyCursor;
296 break;
297 case Qt::LinkAction:
298 shape = Qt::DragLinkCursor;
299 break;
300 default:
301 shape = Qt::ForbiddenCursor;
302 }
303 return QGuiApplicationPrivate::instance()->getPixmapCursor(shape);
304}
305
306/*!
307 Returns the set of possible drop actions for this drag operation.
308
309 \sa exec(), defaultAction()
310*/
311Qt::DropActions QDrag::supportedActions() const
312{
313 Q_D(const QDrag);
314 return d->supported_actions;
315}
316
317
318/*!
319 Returns the default proposed drop action for this drag operation.
320
321 \sa exec(), supportedActions()
322*/
323Qt::DropAction QDrag::defaultAction() const
324{
325 Q_D(const QDrag);
326 return d->default_action;
327}
328
329/*!
330 Cancels a drag operation initiated by Qt.
331
332 \note This is currently implemented on Windows and X11.
333
334 \since 5.7
335 \sa exec()
336*/
337void QDrag::cancel()
338{
339 if (QPlatformDrag *platformDrag = QGuiApplicationPrivate::platformIntegration()->drag())
340 platformDrag->cancelDrag();
341}
342
343/*!
344 \fn void QDrag::actionChanged(Qt::DropAction action)
345
346 This signal is emitted when the \a action associated with the
347 drag changes.
348
349 \sa targetChanged()
350*/
351
352/*!
353 \fn void QDrag::targetChanged(QObject *newTarget)
354
355 This signal is emitted when the target of the drag and drop
356 operation changes, with \a newTarget the new target.
357
358 \sa target(), actionChanged()
359*/
360
361QT_END_NAMESPACE
362
363#include "moc_qdrag.cpp"
Combined button and popup list for selecting options.