QDrag

Inheritance diagram of QDrag

Synopsis

Functions

def actionChanged (action)
def exec_ ([supportedActions=Qt::MoveAction])
def exec_ (supportedActions,defaultAction)
def hotSpot ()
def mimeData ()
def operator= (arg__1)
def pixmap ()
def setDragCursor (cursor,action)
def setHotSpot (hotspot)
def setMimeData (data)
def setPixmap (arg__1)
def source ()
def start ([supportedActions=Qt::CopyAction])
def target ()
def targetChanged (newTarget)

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 QDrag.setMimeData() function in the following way:

drag =  QDrag(self)
mimeData =  QMimeData()

mimeData.setText(commentEdit.toPlainText())
drag.setMimeData(mimeData)

Note that QDrag.setMimeData() assigns ownership of the QMimeData object to the QDrag object. The QDrag must be constructed on the heap with a parent QWidget to ensure that Qt can clean up after the drag and drop operation has been completed.

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 QDrag.setPixmap() . The cursor’s hot spot can be given a position relative to the top-left corner of the pixmap with the QDrag.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,
                       drag.pixmap().height()))

Note

On X11, the pixmap may not be able to keep up with the mouse movements if the hot spot causes the pixmap to be displayed directly under the cursor.

The source and target widgets can be found with QDrag.source() and QDrag.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 QWidget.mousePressEvent() to determine whether the user has pressed a mouse button, and reimplement QWidget.mouseMoveEvent() to check whether a QDrag is required.

See also

Drag and Drop QClipboard QMimeData QWindowsMime QMacPasteboardMime Draggable Icons Example Draggable Text Example Drop Site Example Fridge Magnets Example

class PySide.QtGui.QDrag(dragSource)
Parameter:dragSource – QWidget

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

QDrag.actionChanged(action)
Parameter:action – DropAction
QDrag.exec_([supportedActions=Qt::MoveAction])
Parameter:supportedActions – DropActions
Return type:DropAction
QDrag.exec_(supportedActions, defaultAction)
Parameters:
  • supportedActions – DropActions
  • defaultAction – DropAction
Return type:

DropAction

QDrag.hotSpot()
Return type:QPoint

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

QDrag.mimeData()
Return type:QMimeData

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

QDrag.operator=(arg__1)
Parameter:arg__1 – QDrag
Return type:QDrag
QDrag.pixmap()
Return type:QPixmap

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

QDrag.setDragCursor(cursor, action)
Parameters:
  • cursor – QPixmap
  • action – DropAction
QDrag.setHotSpot(hotspot)
Parameter:hotspot – QPoint

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

Note

on X11, the pixmap may not be able to keep up with the mouse movements if the hot spot causes the pixmap to be displayed directly under the cursor.

See also

QDrag.hotSpot()

QDrag.setMimeData(data)
Parameter:data – QMimeData

Sets the data to be sent to the given MIME data . Ownership of the data is transferred to the QDrag object.

See also

QDrag.mimeData()

QDrag.setPixmap(arg__1)
Parameter:arg__1 – QPixmap

Sets pixmap as the pixmap used to represent the data in a drag and drop operation. You can only set a pixmap before the drag is started.

See also

QDrag.pixmap()

QDrag.source()
Return type:QWidget

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

QDrag.start([supportedActions=Qt::CopyAction])
Parameter:supportedActions – DropActions
Return type:DropAction
QDrag.target()
Return type:QWidget

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

QDrag.targetChanged(newTarget)
Parameter:newTarget – QWidget