0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the documentation of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
/*!
|
|
43 |
\example draganddrop/draggableicons
|
|
44 |
\title Draggable Icons Example
|
|
45 |
|
|
46 |
The Draggable Icons example shows how to drag and drop image data between widgets
|
|
47 |
in the same application, and between different applications.
|
|
48 |
|
|
49 |
\image draggableicons-example.png
|
|
50 |
|
|
51 |
In many situations where drag and drop is used, the user starts dragging from
|
|
52 |
a particular widget and drops the payload onto another widget. In this example,
|
|
53 |
we subclass QLabel to create labels that we use as drag sources, and place them
|
|
54 |
inside \l{QWidget}s that serve as both containers and drop sites.
|
|
55 |
|
|
56 |
In addition, when a drag and drop operation occurs, we want to send more than
|
|
57 |
just an image. We also want to send information about where the user clicked in
|
|
58 |
the image so that the user can place it precisely on the drop target. This level
|
|
59 |
of detail means that we must create a custom MIME type for our data.
|
|
60 |
|
|
61 |
\section1 DragWidget Class Definition
|
|
62 |
|
|
63 |
The icon widgets that we use to display icons are subclassed from QLabel:
|
|
64 |
|
|
65 |
\snippet examples/draganddrop/draggableicons/dragwidget.h 0
|
|
66 |
|
|
67 |
Since the QLabel class provides most of what we require for the icon, we
|
|
68 |
only need to reimplement the \l QWidget::mousePressEvent() to provide
|
|
69 |
drag and drop facilities.
|
|
70 |
|
|
71 |
\section1 DragWidget Class Implementation
|
|
72 |
|
|
73 |
The \c DragWidget constructor sets an attribute on the widget that ensures
|
|
74 |
that it will be deleted when it is closed:
|
|
75 |
|
|
76 |
\snippet examples/draganddrop/draggableicons/dragwidget.cpp 0
|
|
77 |
|
|
78 |
To enable dragging from the icon, we need to act on a mouse press event.
|
|
79 |
We do this by reimplementing \l QWidget::mousePressEvent() and setting up
|
|
80 |
a QDrag object.
|
|
81 |
|
|
82 |
\snippet examples/draganddrop/draggableicons/dragwidget.cpp 1
|
|
83 |
|
|
84 |
Since we will be sending pixmap data for the icon and information about the
|
|
85 |
user's click in the icon widget, we construct a QByteArray and package up the
|
|
86 |
details using a QDataStream.
|
|
87 |
|
|
88 |
For interoperability, drag and drop operations describe the data they contain
|
|
89 |
using MIME types. In Qt, we describe this data using a QMimeData object:
|
|
90 |
|
|
91 |
\snippet examples/draganddrop/draggableicons/dragwidget.cpp 2
|
|
92 |
|
|
93 |
We choose an unofficial MIME type for this purpose, and supply the QByteArray
|
|
94 |
to the MIME data object.
|
|
95 |
|
|
96 |
The drag and drop operation itself is handled by a QDrag object:
|
|
97 |
|
|
98 |
\snippet examples/draganddrop/draggableicons/dragwidget.cpp 3
|
|
99 |
|
|
100 |
Here, we pass the data to the drag object, set a pixmap that will be shown
|
|
101 |
alongside the cursor during the operation, and define the position of a hot
|
|
102 |
spot that places the position of this pixmap under the cursor.
|
|
103 |
|
|
104 |
*/
|