| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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 | */ | 
|---|