Update copyright headers
[qt:qt.git] / doc / src / examples / draggableicons.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file.  Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29     \example draganddrop/draggableicons
30     \title Draggable Icons Example
31
32     \brief The Draggable Icons example shows how to drag and drop image data between widgets
33     in the same application, and between different applications.
34
35     \image draggableicons-example.png
36
37     In many situations where drag and drop is used, the user starts dragging from
38     a particular widget and drops the payload onto another widget. In this example,
39     we subclass QLabel to create labels that we use as drag sources, and place them
40     inside \l{QWidget}s that serve as both containers and drop sites.
41
42     In addition, when a drag and drop operation occurs, we want to send more than
43     just an image. We also want to send information about where the user clicked in
44     the image so that the user can place it precisely on the drop target. This level
45     of detail means that we must create a custom MIME type for our data.
46
47     \section1 DragWidget Class Definition
48
49     The icon widgets that we use to display icons are subclassed from QLabel:
50
51     \snippet examples/draganddrop/draggableicons/dragwidget.h 0
52
53     Since the QLabel class provides most of what we require for the icon, we
54     only need to reimplement the \l QWidget::mousePressEvent() to provide
55     drag and drop facilities.
56
57     \section1 DragWidget Class Implementation
58
59     The \c DragWidget constructor sets an attribute on the widget that ensures
60     that it will be deleted when it is closed:
61
62     \snippet examples/draganddrop/draggableicons/dragwidget.cpp 0
63
64     To enable dragging from the icon, we need to act on a mouse press event.
65     We do this by reimplementing \l QWidget::mousePressEvent() and setting up
66     a QDrag object.
67
68     \snippet examples/draganddrop/draggableicons/dragwidget.cpp 1
69
70     Since we will be sending pixmap data for the icon and information about the
71     user's click in the icon widget, we construct a QByteArray and package up the
72     details using a QDataStream.
73
74     For interoperability, drag and drop operations describe the data they contain
75     using MIME types. In Qt, we describe this data using a QMimeData object:
76
77     \snippet examples/draganddrop/draggableicons/dragwidget.cpp 2
78
79     We choose an unofficial MIME type for this purpose, and supply the QByteArray
80     to the MIME data object.
81
82     The drag and drop operation itself is handled by a QDrag object:
83
84     \snippet examples/draganddrop/draggableicons/dragwidget.cpp 3
85
86     Here, we pass the data to the drag object, set a pixmap that will be shown
87     alongside the cursor during the operation, and define the position of a hot
88     spot that places the position of this pixmap under the cursor.
89
90 */