1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the gui module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
38
\brief The QDrag class provides support for MIME-based drag and drop data
41
Drag and drop is an intuitive way for users to copy or move data around in an
42
application, and is used in many desktop environments as a mechanism for copying
43
data between applications. Drag and drop support in Qt is centered around the
44
QDrag class that handles most of the details of a drag and drop operation.
46
The data to be transferred by the drag and drop operation is contained in a
47
QMimeData object. This is specified with the setMimeData() function in the
50
\quotefromfile snippets/dragging/mainwindow.cpp
51
\skipto mousePressEvent
53
\printuntil setMimeData
55
Note that setMimeData() assigns ownership of the QMimeData object to the
56
QDrag object. The QDrag must be constructed on the heap with a parent QObject
57
to ensure that Qt can clean up after the drag and drop operation has been
60
A pixmap can be used to represent the data while the drag is in progress, and
61
will move with the cursor to the drop target. This pixmap typically shows an
62
icon that represents the MIME type of the data being transferred, but any
63
pixmap can be set with setPixmap(). Care must be taken to ensure that the
64
pixmap is not too large. The cursor's hot spot can be given a position relative
65
to the top-left corner of the pixmap with the setHotSpot() function. The
66
following code positions the pixmap so that the cursor's hot spot points to
67
the center of its bottom edge:
69
\quotefromfile snippets/separations/finalwidget.cpp
73
The source and target widgets can be found with source() and target().
74
These functions are often used to determine whether drag and drop operations
75
started and finished at the same widget, so that special behavior can be
78
QDrag only deals with the drag and drop operation itself. It is up to the
79
developer to decide when a drag operation begins, and how a QDrag object should
80
be constructed and used. For a given widget, it is often necessary to
81
reimplement \l{QWidget::mousePressEvent()}{mousePressEvent()} to determine
82
whether the user has pressed a mouse button, and reimplement
83
\l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} to check whether a QDrag is
86
\sa \link dnd.html Drag and Drop\endlink QClipboard QMimeData
90
Constructs a new drag object for the widget specified by \a dragSource.
92
QDrag::QDrag(QWidget *dragSource)
93
: QObject(*new QDragPrivate, dragSource)
96
d->source = dragSource;
99
d->hotspot = QPoint(-10, -10);
100
d->possible_actions = Qt::CopyAction;
101
d->executed_action = Qt::IgnoreAction;
105
Destroys the drag object.
111
QDragManager *manager = QDragManager::self();
112
if (manager && manager->object == this)
113
manager->cancel(false);
117
Sets the data to be sent to the given MIME \a data. Ownership of the data is
118
transferred to the QDrag object.
120
void QDrag::setMimeData(QMimeData *data)
129
Returns the MIME data that is encapsulated by the drag object.
131
QMimeData *QDrag::mimeData() const
138
Sets \a pixmap as the pixmap used to represent the data in a drag
141
void QDrag::setPixmap(const QPixmap &pixmap)
148
Returns the pixmap used to represent the data in a drag and drop operation.
150
QPixmap QDrag::pixmap() const
157
Sets the position of the hot spot relative to the top-left corner of the
158
pixmap used to the point specified by \a hotspot.
160
void QDrag::setHotSpot(const QPoint& hotspot)
163
d->hotspot = hotspot;
167
Returns the position of the hot spot relative to the top-left corner of the
170
QPoint QDrag::hotSpot() const
177
Returns the source of the drag object. This is the widget where the drag
178
and drop operation originated.
180
QWidget *QDrag::source() const
187
Returns the target of the drag and drop operation. This is the widget where
188
the drag object was dropped.
190
QWidget *QDrag::target() const
197
Starts the drag and drop operation. The actions that are available to the
198
user when the drag and drop operation is completed are specified in
199
\a request. Qt::CopyAction is always allowed.
201
Qt::DropAction QDrag::start(Qt::DropActions request)
204
Q_ASSERT_X(d->data, "QDrag", "No mimedata set before starting the drag");
205
QDragManager *manager = QDragManager::self();
206
d->possible_actions = request | Qt::CopyAction;
208
d->executed_action = manager->drag(this);
209
return d->executed_action;
213
Sets the drag \a cursor for the \a action. This alows you
214
to overide the defualt native cursors. To revert to using the
215
native cursor for \a action pass in a null QPixmap as \a cursor.
217
The \a action can only be CopyAction, MoveAction or LinkAction.
218
All other values of DropAction are ignored.
220
void QDrag::setDragCursor(const QPixmap &cursor, Qt::DropAction action)
223
if (action != Qt::CopyAction && action != Qt::MoveAction && action != Qt::LinkAction)
226
d->customCursors.remove(action);
228
d->customCursors[action] = cursor;
232
\fn void QDrag::actionChanged(Qt::DropAction action)
234
This signal is emitted when the \a action associated with the
241
\fn void QDrag::targetChanged(QWidget *newTarget)
243
This signal is emitted when the target of the drag and drop
244
operation changes, with \a newTarget the new target.
246
\sa target(), actionChanged()