PySide.QtGui.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 PySide.QtGui.QDrag class that handles most of the details of a drag and drop operation.
拖放操作要转移的数据包含在 PySide.QtCore.QMimeData 对象。指定这采用 PySide.QtGui.QDrag.setMimeData() function in the following way:
drag = QDrag(self)
mimeData = QMimeData()
mimeData.setText(commentEdit.toPlainText())
drag.setMimeData(mimeData)
注意: PySide.QtGui.QDrag.setMimeData() assigns ownership of the PySide.QtCore.QMimeData object to the PySide.QtGui.QDrag 对象。 PySide.QtGui.QDrag must be constructed on the heap with a parent PySide.QtGui.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 PySide.QtGui.QDrag.setPixmap() . The cursor's hot spot can be given a position relative to the top-left corner of the pixmap with the PySide.QtGui.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()))
注意
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 采用 PySide.QtGui.QDrag.source() and PySide.QtGui.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.
PySide.QtGui.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 PySide.QtGui.QDrag object should be constructed and used. For a given widget, it is often necessary to reimplement PySide.QtGui.QWidget.mousePressEvent() to determine whether the user has pressed a mouse button, and reimplement PySide.QtGui.QWidget.mouseMoveEvent() to check whether a PySide.QtGui.QDrag is required.
另请参阅
拖放 PySide.QtGui.QClipboard PySide.QtCore.QMimeData QWindowsMime QMacPasteboardMime 可拖曳图标范例 可拖曳文本范例 掉落站点范例 冰箱磁贴范例
| 参数: | dragSource – PySide.QtGui.QWidget |
|---|
为小部件构造新的拖拽对象指定通过 dragSource .
| 参数: | action – PySide.QtCore.Qt.DropAction |
|---|
| 参数: | supportedActions – PySide.QtCore.Qt.DropActions |
|---|---|
| 返回类型: | PySide.QtCore.Qt.DropAction |
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.Qt.DropAction |
| 返回类型: | PySide.QtCore.QPoint |
|---|
返回相对于光标左上角的热点位置。
| 返回类型: | PySide.QtCore.QMimeData |
|---|
返回由拖曳对象封装的 MIME 数据。
| 返回类型: | PySide.QtGui.QPixmap |
|---|
返回用于在拖放操作中表示数据的像素图。
| 参数: |
|
|---|
| 参数: | hotspot – PySide.QtCore.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.
| 参数: | data – PySide.QtCore.QMimeData |
|---|
将要发送数据设为给定 MIME data 。数据的所有权被转移给 PySide.QtGui.QDrag 对象。
| 参数: | arg__1 – PySide.QtGui.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.
| 返回类型: | PySide.QtGui.QWidget |
|---|
Returns the source of the drag object. This is the widget where the drag and drop operation originated.
| 参数: | supportedActions – PySide.QtCore.Qt.DropActions |
|---|---|
| 返回类型: | PySide.QtCore.Qt.DropAction |
| 返回类型: | PySide.QtGui.QWidget |
|---|
Returns the target of the drag and drop operation. This is the widget where the drag object was dropped.
| 参数: | newTarget – PySide.QtGui.QWidget |
|---|