Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QDrag Class Reference

The QDrag class provides support for MIME-based drag and drop data transfer. More...

    #include <QDrag>

Inherits QObject.

- List of all members, including inherited members

### Public Types

- enum DropAction { CopyAction, MoveAction, LinkAction, ActionMask, TargetMoveAction, IgnoreAction }
- typedef DropActions

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QDrag ( QWidget * dragSource )
- ~QDrag ()
- QPoint hotSpot () const
- QMimeData * mimeData () const
- QPixmap pixmap () const
- void setHotSpot ( const QPoint & hotspot )
- void setMimeData ( QMimeData * data )
- void setPixmap ( const QPixmap & pixmap )
- QWidget * source () const
- DropAction start ( DropActions supportedActions = CopyAction )
- QWidget * target () const

- 26 public functions inherited from QObject

### Public Slots

- 1 public slot inherited from QObject

### Signals

- void actionChanged ( DropAction action )
- void targetChanged ( QWidget * newTarget )

- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- 7 protected functions inherited from QObject

## Detailed Description

The QDrag class provides support for MIME-based drag and drop data transfer.

Drag and drop is an intuitive way for users to copy or move data around in an application, and is used in many desktop environments as a mechanism for copying data between applications. Drag and drop support in Qt is centered around the QDrag class that handles most of the details of a drag and drop operation.

The data to be transferred by the drag and drop operation is contained in a QMimeData object. This is specified with the setMimeData() function in the following way:

            QDrag *drag = new QDrag(this);
            QMimeData *mimeData = new QMimeData;

            mimeData->setText(commentEdit->toPlainText());
            drag->setMimeData(mimeData);

Note that setMimeData() assigns ownership of the QMimeData object to the QDrag object. The QDrag must be constructed on the heap and given a parent QObject to ensure that Qt can perform the necessary memory management operations on the objects the next time the event loop is entered.

A pixmap can be used to represent the data while the drag is in progress, and will move with the cursor to the drop target. This pixmap typically shows an icon that represents the MIME type of the data being transferred, but any pixmap can be set with setPixmap(). Care must be taken to ensure that the pixmap is not too large. The cursor's hot spot can be given a position relative to the top-left corner of the pixmap with the setHotSpot() function. The following code positions the pixmap so that the cursor's hot spot points to the center of its bottom edge:

            drag->setHotSpot(QPoint(drag->pixmap().width()/2,

The source and target widgets can be found with source() and target(). These functions are often used to determine whether drag and drop operations started and finished at the same widget, so that special behavior can be implemented.

QDrag only deals with the drag and drop operation itself. It is up to the developer to decide when a drag operation begins, and how a QDrag object should be constructed and used. For a given widget, it is often necessary to reimplement mousePressEvent() to determine whether the user has pressed a mouse button, and reimplement mouseMoveEvent() to check whether a QDrag is required.

See also Drag and Drop, QClipboard, and QMimeData.

## Member Type Documentation

### enum QDrag::DropAction
typedef QDrag::DropActions

`QDrag::CopyAction` | Copy the data to the target.
`QDrag::MoveAction` | Move the date from the source to the target.
`QDrag::LinkAction` | Create a link from the source to the target.
`QDrag::ActionMask` | ###
`QDrag::TargetMoveAction` | ###
`QDrag::IgnoreAction` | Ignore the action (do nothing with the data).

The DropActions typedef can store a combination of DropAction values.

## Member Function Documentation

### QDrag::QDrag ( QWidget * dragSource )

Constructs a new drag object for the widget specified by dragSource.

### QDrag::~QDrag ()

Destroys the drag object.

### void QDrag::actionChanged ( DropAction action ) `[signal]`

### QPoint QDrag::hotSpot () const

Returns the position of the hot spot relative to the top-left corner of the cursor.

### QMimeData * QDrag::mimeData () const

Returns the MIME data that is encapsulated by the drag object.

### QPixmap QDrag::pixmap () const

Returns the pixmap used to represent the data in a drag and drop operation.

### void QDrag::setHotSpot ( const QPoint & hotspot )

Sets the position of the hot spot relative to the top-left corner of the pixmap used to the point specified by hotspot.

### void QDrag::setMimeData ( QMimeData * data )

Sets the data to be sent to the given MIME data.

### void QDrag::setPixmap ( const QPixmap & pixmap )

Sets pixmap as the pixmap used to represent the data in a drag and drop operation.

### QWidget * QDrag::source () const

Returns the source of the drag object. This is the widget where the drag and drop operation originated.

### DropAction QDrag::start ( DropActions supportedActions = CopyAction )

### QWidget * QDrag::target () const

Returns the target of the drag and drop operation. This is the widget where the drag object was dropped.

### void QDrag::targetChanged ( QWidget * newTarget ) `[signal]`