本页内容

QDropEvent Class

QDropEvent 类提供了一个事件,该事件在拖放操作完成时触发。更多内容...

头文件: #include <QDropEvent>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承自: QEvent
被继承者:

QDragMoveEvent

公共函数

QDropEvent(const QPointF &pos, Qt::DropActions actions, const QMimeData *data, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, QEvent::Type type = Drop)
void acceptProposedAction()
(since 6.0) Qt::MouseButtons buttons() const
Qt::DropAction dropAction() const
const QMimeData *mimeData() const
(since 6.0) Qt::KeyboardModifiers modifiers() const
(since 6.0) QPointF position() const
Qt::DropActions possibleActions() const
Qt::DropAction proposedAction() const
void setDropAction(Qt::DropAction action)
QObject *source() const

受保护函数

QDropEvent(QDropEvent &&)
QDropEvent &operator=(QDropEvent &&)
QDropEvent &operator=(const QDropEvent &other)

详细说明

当小部件accepts drop events 时,如果它已接受发送给它的最新QDragEnterEvent 或QDragMoveEvent ,则会收到此事件。

拖放事件包含一个建议的操作,可通过proposedAction() 获取,小部件可以接受或忽略该操作。 如果小部件能够处理该操作,则应调用acceptProposedAction() 函数。由于建议的操作可能是多个Qt::DropAction 值的组合,因此可以选择其中一个值作为默认操作,或者让用户选择其首选操作。

如果建议的拖放操作不合适(例如因为您的自定义控件不支持该操作),您可以通过调用setDropAction()并传入首选操作,将其替换为possible drop actions 中的任意操作。 如果您设置的值不在possibleActions() 返回的值的按位或组合中,则将使用默认的复制操作。一旦设置了替代的拖放操作,请调用accept() 而不是acceptProposedAction() 来完成拖放操作。

mimeData() 函数通过一个QMimeData 对象提供拖放到控件上的数据。该对象除了包含数据本身外,还包含有关数据 MIME 类型的信息。

另请参阅 QMimeData 、QDrag 以及“拖放”功能。

成员函数文档

QDropEvent::QDropEvent(const QPointF &pos, Qt::DropActions actions, const QMimeData *data, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, QEvent::Type type = Drop)

构建一个特定type 的拖放事件,该事件对应于在目标控件坐标系中pos 指定的点处的拖放操作。

actions 指定了可以执行的拖放操作类型,而拖拽数据则以 MIME 编码形式存储在data 中。

释放时的鼠标按钮和键盘修饰键状态由buttons 和modifiers 指定。

[protected delete] QDropEvent::QDropEvent(QDropEvent &&)

通过移动构造方式创建一个QDropEvent 的实例。该函数已被删除。

void QDropEvent::acceptProposedAction()

将拖放操作设置为建议的操作。

另请参阅 setDropAction()、proposedAction() 和accept()。

[since 6.0] Qt::MouseButtons QDropEvent::buttons() const

返回当前按下的鼠标按钮。

该函数于 Qt 6.0 中引入。

Qt::DropAction QDropEvent::dropAction() const

返回目标将对数据执行的操作。如果您已调用 `setDropAction()` 显式选择了“拖放”操作,则此操作可能与 `proposedAction()` 中提供的操作不同。

另请参阅 setDropAction()。

const QMimeData *QDropEvent::mimeData() const

返回拖放到控件上的数据及其相关的 MIME 类型信息。

[since 6.0] Qt::KeyboardModifiers QDropEvent::modifiers() const

返回当前按下的修饰键。

该函数在 Qt 6.0 中引入。

[since 6.0] QPointF QDropEvent::position() const

返回拖放操作发生的位置。

该函数在 Qt 6.0 中引入。

Qt::DropActions QDropEvent::possibleActions() const

返回可能的下放操作的“或”组合。

另请参阅 dropAction()。

Qt::DropAction QDropEvent::proposedAction() const

返回建议的删除操作。

另请参阅 dropAction()。

void QDropEvent::setDropAction(Qt::DropAction action)

设置目标对数据执行的action 。使用此选项可将proposed action 替换为possible actions 中的某一项。

如果设置的释放操作不在可用操作列表中,则拖放操作将默认执行复制操作。

一旦提供了替代的释放操作,请调用accept() 代替acceptProposedAction()。

另请参阅 dropAction()。

QObject *QDropEvent::source() const

如果拖动操作的源是本应用程序中的一个控件,则该函数返回该源;否则返回nullptr 。操作的源是用于实例化拖动操作的QDrag 对象的第一个参数。

当小部件在被拖动至自身时需要特殊行为时,此功能非常有用。

另请参阅 QDrag::QDrag()。

[protected delete] QDropEvent &QDropEvent::operator=(QDropEvent &&)

将other 按移动赋值方式赋值给此QDropEvent 实例。该函数已被删除。

[noexcept protected default] QDropEvent &QDropEvent::operator=(const QDropEvent &other)

将other 复制并赋值给此QDropEvent 实例。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.