内容表

上一话题

QDoubleValidator

下一话题

QDragEnterEvent

QDrag

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

Inheritance diagram of PySide2.QtGui.QDrag

概要

函数

信号

静态函数

详细描述

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.

拖放操作要转移的数据包含在 QMimeData 对象。指定这采用 setMimeData() function in the following way:

drag =  QDrag(self)
mimeData =  QMimeData()
mimeData.setText(commentEdit.toPlainText())
drag.setMimeData(mimeData)
											

注意: setMimeData() assigns ownership of the QMimeData object to the QDrag 对象。 QDrag must be constructed on the heap with a parent QObject 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 setPixmap() . 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,
                        drag->pixmap().height()));
											

注意

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.

可以找到源和目标 Widget 采用 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.

class QDrag ( dragSource )
param dragSource

QObject

为小部件构造新的拖拽对象指定通过 dragSource .

PySide2.QtGui.QDrag. actionChanged ( action )
参数

action DropAction

static PySide2.QtGui.QDrag. cancel ( )

取消由 Qt 初始的拖拽操作。

注意

目前这在 Windows 和 X11 上有实现。

另请参阅

exec()

PySide2.QtGui.QDrag. defaultAction ( )
返回类型

DropAction

返回此拖曳操作的默认提议拖曳动作。

另请参阅

exec() supportedActions()

PySide2.QtGui.QDrag. dragCursor ( action )
参数

action DropAction

返回类型

QPixmap

返回拖曳光标为 action .

另请参阅

setDragCursor()

PySide2.QtGui.QDrag. exec_ ( [ supportedActions=Qt.MoveAction ] )
参数

supportedActions DropActions

返回类型

DropAction

Starts the drag and drop operation and returns a value indicating the requested drop action when it is completed. The drop actions that the user can choose from are specified in supportedActions . The default proposed action will be selected among the allowed actions in the following order: Move, Copy and Link.

注意

On Linux and macOS, the drag and drop operation can take some time, but this function does not block the event loop. Other events are still delivered to the application while the operation is performed. On Windows, the Qt event loop is blocked during the operation.

另请参阅

cancel()

PySide2.QtGui.QDrag. exec_ ( supportedActions , defaultAction )
参数
  • supportedActions DropActions

  • defaultAction DropAction

返回类型

DropAction

Starts the drag and drop operation and returns a value indicating the requested drop action when it is completed. The drop actions that the user can choose from are specified in supportedActions .

defaultDropAction determines which action will be proposed when the user performs a drag without using modifier keys.

注意

On Linux and macOS, the drag and drop operation can take some time, but this function does not block the event loop. Other events are still delivered to the application while the operation is performed. On Windows, the Qt event loop is blocked during the operation. However, exec() on Windows causes processEvents() to be called frequently to keep the GUI responsive. If any loops or operations are called while a drag operation is active, it will block the drag operation.

PySide2.QtGui.QDrag. hotSpot ( )
返回类型

QPoint

返回相对于光标左上角的热点位置。

另请参阅

setHotSpot()

PySide2.QtGui.QDrag. mimeData ( )
返回类型

QMimeData

返回由拖曳对象封装的 MIME 数据。

另请参阅

setMimeData()

PySide2.QtGui.QDrag. pixmap ( )
返回类型

QPixmap

返回用于在拖放操作中表示数据的像素图。

另请参阅

setPixmap()

PySide2.QtGui.QDrag. setDragCursor ( cursor , action )
参数

设置拖曳 cursor action . This allows you to override the default native cursors. To revert to using the native cursor for action pass in a null QPixmap as cursor .

Note: setting the drag cursor for IgnoreAction may not work on all platforms. X11 and macOS has been tested to work. Windows does not support it.

另请参阅

dragCursor()

PySide2.QtGui.QDrag. setHotSpot ( hotspot )
参数

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 .

注意

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.

另请参阅

hotSpot()

PySide2.QtGui.QDrag. setMimeData ( data )
参数

data QMimeData

将要发送数据设为给定 MIME data 。数据的所有权被转移给 QDrag 对象。

另请参阅

mimeData()

PySide2.QtGui.QDrag. setPixmap ( arg__1 )
参数

arg__1 QPixmap

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.

另请参阅

pixmap()

PySide2.QtGui.QDrag. source ( )
返回类型

QObject

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

PySide2.QtGui.QDrag. start ( [ supportedActions=Qt.CopyAction ] )
参数

supportedActions DropActions

返回类型

DropAction

注意

此函数被弃用。

注意

It is recommended to use exec() instead of this function.

Starts the drag and drop operation and returns a value indicating the requested drop action when it is completed. The drop actions that the user can choose from are specified in request . CopyAction is always allowed.

注意

Although the drag and drop operation can take some time, this function does not block the event loop. Other events are still delivered to the application while the operation is performed.

另请参阅

exec()

PySide2.QtGui.QDrag. supportedActions ( )
返回类型

DropActions

Returns the set of possible drop actions for this drag operation.

另请参阅

exec() defaultAction()

PySide2.QtGui.QDrag. target ( )
返回类型

QObject

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

PySide2.QtGui.QDrag. targetChanged ( newTarget )
参数

newTarget QObject