[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example draganddrop/draggableicons
|
---|
| 30 | \title Draggable Icons Example
|
---|
| 31 |
|
---|
| 32 | 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 | */
|
---|